Php 删除链接后的文本
因此,我在我的网站上有一个Php 删除链接后的文本,php,regex,preg-replace,Php,Regex,Preg Replace,因此,我在我的网站上有一个@提及功能,用户可以自己输入,但可以执行以下操作: @福你好,这是一些提及文字包括在内 我只想删除文本(在@foo之后的所有内容),内容通过streamitem\u content: $json['streamitem_content_usertagged'] = preg_replace('/(^|\s)@(\w+)/', '\1@<a href="profile.php?username=$1">$1</a>', $json['streami
@提及
功能,用户可以自己输入,但可以执行以下操作:
@福你好,这是一些提及文字包括在内
我只想删除文本(在@foo之后的所有内容),内容通过streamitem\u content
:
$json['streamitem_content_usertagged'] =
preg_replace('/(^|\s)@(\w+)/', '\1@<a href="profile.php?username=$1">$1</a>',
$json['streamitem_content']);
$json['streamitem\u content\u usertaged']=
preg_replace('/(^ |\s)@(\w+)/','\1@',
$json['streamitem_content']);
您可以使用以下功能:
preg_replace('/^\s*@(\w+)/', '<a href="profile.php?username=$1">@$1</a>',
$json['streamitem_content']);
preg_replace('/^\s*@(\w+/),'',
$json['streamitem_content']);
这将删除前导空格,并在超链接文本中包含@(而不是link参数)
如果您需要保持前导空格的圆滑:
preg_replace('/^(\s*)@(\w+)/', '$1<a href="profile.php?username=$2">@$2</a>',
$json['streamitem_content']);
preg_replace('/^(\s*)@(\w+/),“$1”,
$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=$1">$1</a>',
$json['streamitem_content']);
echo $json['streamitem_content_usertagged'];
实际上是
preg_replace('/(^|\s)@(\w+)/', '$1@<a href="profile.php?username=$2">$2</a>',
$json['streamitem_content']);
preg_replace('/(^|\s)@(\w+/”,“$1@”,
$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=$1">$1</a>',
$json['streamitem_content']);
echo $json['streamitem_content_usertagged'];
$json['streamitem_content']='@foo Hello这是一些提及的文字;
$json['streamitem\u content\u usertaged']=
preg_replace('/@(\w+).*$/','@',
$json['streamitem_content']);
echo$json['streamitem_content_usertaged'];
输出:
@<a href="profile.php?username=foo">foo</a> Hello This is some mention text included.
@<a href="profile.php?username=foo">foo</a>
@
如果要在@foo
之后替换的内容可以扩展到多行,请使用s
Regex101演示:
因此,正则表达式说,找到一个
@
,然后捕获所有连续的a-zA-Z0-9
字符。在这些连续字符之后,我们不介意转到字符串的末尾。您可以使用explode()代码>和str_replace()代码>。它们可能比preg
具有速度优势
假设该行作为变量可用(例如,$提提
):
$title=$json['streamitem_content'];
$title_parts=爆炸(“,$title”);
$the_part_you_want=str_replace('@','$they_parts[0]);
//或者您可以使用$the_part_you_want=ltrim($title_parts[0],“@”);
$json['streamitem\u content\u usertaged']='@';
或使用trim($note_parts[0])
删除任何不需要的空白
您可以使用更少的变量并将$提提
重用为数组,但这似乎是一种更清晰的方法来说明原理。对不起,我认为我不清楚。我只是不想在“@fooSo just@text
之后出现文本,直到出现空白,然后删除所有其他内容?更新了问题,这样其他人就不会感到困惑。很抱歉,Chris,您的代码似乎无法解决问题,并且文本仍然包含在@fooThis之后。这是我在firebug中收到的更新代码{“streamitem\u id:“1086”,“streamitem\u内容:“Luce dddd”,
@<a href="profile.php?username=foo">foo</a>
$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>';