Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复这个字符串替换php问题?_Php_String_Replace - Fatal编程技术网

如何修复这个字符串替换php问题?

如何修复这个字符串替换php问题?,php,string,replace,Php,String,Replace,我想用链接替换单词(不包括:,;.stc.)。我该怎么做 <?php $string = "wordey; string, boom"; $string = preg_replace("/[^a-z]/i", "<a href='x'>/[^a-z]/i</a>", $string); //?? echo $string; // <a href='wordey'>wordey</a>; <a href='string'>strin

我想用链接替换单词(不包括:,;.stc.)。我该怎么做

<?php
$string = "wordey; string, boom";
$string = preg_replace("/[^a-z]/i", "<a href='x'>/[^a-z]/i</a>", $string); //??
echo $string; // <a href='wordey'>wordey</a>; <a href='string'>string</a>, <a href='boom'>boom</a>
?>


请注意;,。-等等都很重要

正则表达式或替换字符串都没有意义。正则表达式正在匹配范围
[a-z]
以外的所有内容(由前导的
^
表示),并且替换字符串似乎包含正则表达式语法,而它不应该包含该语法

如果您试图替换这些单词,正则表达式可能看起来像
/[a-z]+//i
,它对一个或多个字母进行不区分大小写的贪婪匹配

要在替换中使用匹配的字符串,可以使用
\N
,其中
N
是一个数字,指示要引用的子匹配。若要添加子匹配,请在您感兴趣的正则表达式引用部分周围放置括号。正则表达式变成
/([a-z]+)/i

把它们放在一起,你会得到以下结果,这似乎给出了你想要的结果

$string = preg_replace("/([a-z]+)/i", "<a href='\\1'>\\1</a>", $string);
$string=preg_replace(“/([a-z]+)/i”,”,$string);

注意,双反斜杠是一个转义序列,在字符串中插入一个文字反斜杠。

正则表达式或替换字符串都没有意义。正则表达式正在匹配范围
[a-z]
以外的所有内容(由前导的
^
表示),并且替换字符串似乎包含正则表达式语法,而它不应该包含该语法

$string = preg_replace('/(\w+)/', '<a href="\\1">\\1</a>', $string);
如果您试图替换这些单词,正则表达式可能看起来像
/[a-z]+//i
,它对一个或多个字母进行不区分大小写的贪婪匹配

要在替换中使用匹配的字符串,可以使用
\N
,其中
N
是一个数字,指示要引用的子匹配。若要添加子匹配,请在您感兴趣的正则表达式引用部分周围放置括号。正则表达式变成
/([a-z]+)/i

把它们放在一起,你会得到以下结果,这似乎给出了你想要的结果

$string = preg_replace("/([a-z]+)/i", "<a href='\\1'>\\1</a>", $string);
$string=preg_replace(“/([a-z]+)/i”,”,$string);
注意,双反斜杠是一个转义序列,在字符串中插入一个文字反斜杠。

$string=preg_replace('/(\w+/),'$string);
$string = preg_replace('/(\w+)/', '<a href="\\1">\\1</a>', $string);
$string=preg_replace(“/(\w+/”,“$string”);
试试这个


试试这个


试试这个:

$result = preg_replace('/([^;,\\s]+)/', '<a href="$1">$1</a>', $subject);
$result=preg_replace(“/([^;,\\s]+)/”,“,”$subject);
试试这个:

$result = preg_replace('/([^;,\\s]+)/', '<a href="$1">$1</a>', $subject);
$result=preg_replace(“/([^;,\\s]+)/”,“,”$subject);