删除 link 之后的文字
Remove text after link
所以我的网站上有一个 @mentions
功能,用户可以自己输入但可以执行以下操作:
@foo Hello This is some mention text included.
我只想删除文本(@foo 之后的所有内容)内容来自 streamitem_content
:
$json['streamitem_content_usertagged'] =
preg_replace('/(^|\s)@(\w+)/', '@<a href="profile.php?username="></a>',
$json['streamitem_content']);
你可以使用这个:
preg_replace('/^\s*@(\w+)/', '<a href="profile.php?username=">@</a>',
$json['streamitem_content']);
这会删除前导白色 space,并在 hyperlink 的文本中包含 @(而不是 link 参数)。
如果您需要保持前导白色 space 完整:
preg_replace('/^(\s*)@(\w+)/', '<a href="profile.php?username=">@</a>',
$json['streamitem_content']);
试试这个
$json['streamitem_content'] = '@foo Hello This is some mention text included.';
$json['streamitem_content_usertagged'] =
preg_replace('/@(\w+)/', '@<a href="profile.php?username="></a>',
$json['streamitem_content']);
echo $json['streamitem_content_usertagged'];
输出:
@<a href="profile.php?username=foo">foo</a> Hello This is some mention text included.
Preg_replace
只会替换它找到的内容,因此您无需查找您不感兴趣的内容。如果您确实想捕获字符串的多个部分,尽管捕获组在每个组后增加一个 ()
。所以这个
preg_replace('/(^|\s)@(\w+)/', '@<a href="profile.php?username="></a>',
$json['streamitem_content']);
echo $json['streamitem_content_usertagged'];
实际上是
preg_replace('/(^|\s)@(\w+)/', '@<a href="profile.php?username="></a>',
$json['streamitem_content']);
更新:
$json['streamitem_content'] = '@foo Hello This is some mention text included.';
$json['streamitem_content_usertagged'] =
preg_replace('/@(\w+).*$/', '@<a href="profile.php?username="></a>',
$json['streamitem_content']);
echo $json['streamitem_content_usertagged'];
输出:
@<a href="profile.php?username=foo">foo</a>
如果@foo
之后要替换的内容可以扩展到多行使用s
modifier.
Regex101 演示:https://regex101.com/r/tX1rO0/1
正则表达式几乎说找到一个 @
然后捕获所有连续的 a-zA-Z0-9_
字符。在那些我们不关心的连续字符之后转到字符串的末尾。
您可以使用 explode();
和 str_replace();
。他们可能比 preg
.
有速度优势
假设该行可作为变量使用(例如 $mention
):
$mention = $json['streamitem_content'];
$mention_parts = explode(" ", $mention);
$the_part_you_want = str_replace('@','', $mention_parts[0]);
// or you could use $the_part_you_want = ltrim($mention_parts[0], '@');
$json['streamitem_content_usertagged'] = '@<a href="profile.php?username=' . $the_part_you_want . '">' . $mention_parts[0] . '</a>';
或使用 trim($mention_parts[0]);
删除任何不需要的空格。
您可以使用更少的变量并将 $mention
重用为数组,但这似乎是说明原理的更清晰的方法。
所以我的网站上有一个 @mentions
功能,用户可以自己输入但可以执行以下操作:
@foo Hello This is some mention text included.
我只想删除文本(@foo 之后的所有内容)内容来自 streamitem_content
:
$json['streamitem_content_usertagged'] =
preg_replace('/(^|\s)@(\w+)/', '@<a href="profile.php?username="></a>',
$json['streamitem_content']);
你可以使用这个:
preg_replace('/^\s*@(\w+)/', '<a href="profile.php?username=">@</a>',
$json['streamitem_content']);
这会删除前导白色 space,并在 hyperlink 的文本中包含 @(而不是 link 参数)。
如果您需要保持前导白色 space 完整:
preg_replace('/^(\s*)@(\w+)/', '<a href="profile.php?username=">@</a>',
$json['streamitem_content']);
试试这个
$json['streamitem_content'] = '@foo Hello This is some mention text included.';
$json['streamitem_content_usertagged'] =
preg_replace('/@(\w+)/', '@<a href="profile.php?username="></a>',
$json['streamitem_content']);
echo $json['streamitem_content_usertagged'];
输出:
@<a href="profile.php?username=foo">foo</a> Hello This is some mention text included.
Preg_replace
只会替换它找到的内容,因此您无需查找您不感兴趣的内容。如果您确实想捕获字符串的多个部分,尽管捕获组在每个组后增加一个 ()
。所以这个
preg_replace('/(^|\s)@(\w+)/', '@<a href="profile.php?username="></a>',
$json['streamitem_content']);
echo $json['streamitem_content_usertagged'];
实际上是
preg_replace('/(^|\s)@(\w+)/', '@<a href="profile.php?username="></a>',
$json['streamitem_content']);
更新:
$json['streamitem_content'] = '@foo Hello This is some mention text included.';
$json['streamitem_content_usertagged'] =
preg_replace('/@(\w+).*$/', '@<a href="profile.php?username="></a>',
$json['streamitem_content']);
echo $json['streamitem_content_usertagged'];
输出:
@<a href="profile.php?username=foo">foo</a>
如果@foo
之后要替换的内容可以扩展到多行使用s
modifier.
Regex101 演示:https://regex101.com/r/tX1rO0/1
正则表达式几乎说找到一个 @
然后捕获所有连续的 a-zA-Z0-9_
字符。在那些我们不关心的连续字符之后转到字符串的末尾。
您可以使用 explode();
和 str_replace();
。他们可能比 preg
.
假设该行可作为变量使用(例如 $mention
):
$mention = $json['streamitem_content'];
$mention_parts = explode(" ", $mention);
$the_part_you_want = str_replace('@','', $mention_parts[0]);
// or you could use $the_part_you_want = ltrim($mention_parts[0], '@');
$json['streamitem_content_usertagged'] = '@<a href="profile.php?username=' . $the_part_you_want . '">' . $mention_parts[0] . '</a>';
或使用 trim($mention_parts[0]);
删除任何不需要的空格。
您可以使用更少的变量并将 $mention
重用为数组,但这似乎是说明原理的更清晰的方法。