Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.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 put<;em>;及</em>;在每个关键字的开头和结尾找到_Php_String_Pcre - Fatal编程技术网

Php put<;em>;及</em>;在每个关键字的开头和结尾找到

Php put<;em>;及</em>;在每个关键字的开头和结尾找到,php,string,pcre,Php,String,Pcre,我是preg的新手,想在数组中找到一些字符串,并强调每个字符串 例如 数组[0]=“windows”; 数组[0]=“windows xp”; 文本将是:windows是bla bla…windows xp是bla bla bla 我该怎么做呢?非常简单的更换,不需要preg.*: $mytest = "Windows XP was all right, but I like Windows 7 better!°"; $replacement = "Windows XP"; echo str_

我是preg的新手,想在数组中找到一些字符串,并强调每个字符串

例如

数组[0]=“windows”;
数组[0]=“windows xp”;
文本将是:windows是bla bla…windows xp是bla bla bla

我该怎么做呢?

非常简单的更换,不需要preg.*:

$mytest = "Windows XP was all right, but I like Windows 7 better!°";
$replacement = "Windows XP";
echo str_replace($replacement, "<em>".$replacement."</em>", $mytest);
$mytest=“Windows XP还可以,但我更喜欢Windows 7!”;
$replacement=“Windows XP”;
echo str_replace($replacement,“$replacement.”,$mytest);

您还可以替换阵列。[手动输入][1]

非常简单的更换,无需预录*:

$mytest = "Windows XP was all right, but I like Windows 7 better!°";
$replacement = "Windows XP";
echo str_replace($replacement, "<em>".$replacement."</em>", $mytest);
$mytest=“Windows XP还可以,但我更喜欢Windows 7!”;
$replacement=“Windows XP”;
echo str_replace($replacement,“$replacement.”,$mytest);

您还可以替换阵列。[手动输入][1]

如果您正在对文本进行搜索和替换,这很容易:

$text = 'windows xp';
$input = '....';
$output = str_replace($text, '<em>' . $text . '</em>', $input);
$text='windows xp';
$input='..';
$output=str_replace($text、.$text.'、$input);
如果将一块HTML存储为字符串,则需要考虑另一个问题:如果用户搜索的文本是标记或属性名称或值的一部分,该怎么办?如果要突出显示“strong”,则不希望替换此选项:

<strong>some text</strong>
一些文本
与:

一些文本
因为您的标记将不再有效

这就是在标记上使用简单搜索替换或正则表达式的问题。处理此问题的最佳方法是将标记转换为DOM树,然后遍历它在文本节点上进行替换。即使这样也有问题。如果要突出显示“windows xp”,是否要突出显示此案例:

windows <i>xp</i>
windowsxp

因此,它显示为“windows xp”,因此可以合理地假设用户希望突出显示它。

如果您在文本上进行搜索和替换,这很容易:

$text = 'windows xp';
$input = '....';
$output = str_replace($text, '<em>' . $text . '</em>', $input);
$string = preg_replace("(windows( xp)?)", "<em>$0</em>", $string)
$text='windows xp';
$input='..';
$output=str_replace($text、.$text.'、$input);
如果将一块HTML存储为字符串,则需要考虑另一个问题:如果用户搜索的文本是标记或属性名称或值的一部分,该怎么办?如果要突出显示“strong”,则不希望替换此选项:

<strong>some text</strong>
一些文本
与:

一些文本
因为您的标记将不再有效

这就是在标记上使用简单搜索替换或正则表达式的问题。处理此问题的最佳方法是将标记转换为DOM树,然后遍历它在文本节点上进行替换。即使这样也有问题。如果要突出显示“windows xp”,是否要突出显示此案例:

windows <i>xp</i>
windowsxp
因此,它显示为“windows xp”,因此可以合理地假设用户希望突出显示它。

$string=preg\u replace(((windows(xp)?)”、“$0”、$string)
$string = preg_replace("(windows( xp)?)", "<em>$0</em>", $string)
$string=preg_replace(“(windows(xp)”,“$0”,$string)

这是您想要的搜索和替换类型吗?如果是纯文本还是HTML?它是一个主体,我想用php找到它。用户输入一个关键字,我将其高亮显示。这是您想要的搜索和替换类型吗?如果是纯文本还是HTML?它是一个主体,我想用php找到它。用户输入一个关键字,我将其高亮显示。$string='windowsxp'$字符串=“”$字符串';echo$字符串;对不起。我更新了我的问题,因为我注意到我想在数组中找到多个字符串,而不是仅仅替换一个。但是如果替换的是HTML,最好更新你的问题。这有很多复杂之处。$string='windowsxp'$字符串=“”$字符串';echo$字符串;对不起。我更新了我的问题,因为我注意到我想在数组中找到多个字符串,而不是仅仅替换一个。但是如果替换的是HTML,最好更新你的问题。这有很多复杂之处。我想在一个文本中搜索我在一个数组中得到的关键字,并将它们全部高亮显示。我看起来有很多事情要考虑,所以我不运行HTML标签的ItP问题。查看我的更新帖子。我想在一个文本中搜索我在数组中得到的关键字,并将它们全部高亮显示。我看起来有很多事情要考虑,所以我不运行HTML标签的ItP问题。请参阅我的最新帖子。。