如何在srting中查找子字符串,如果为true,如何在PHP中在中间插入文本?

如何在srting中查找子字符串,如果为true,如何在PHP中在中间插入文本?,php,string,Php,String,我想在给定的字符串中搜索特定的子字符串,如果找到了,我只想在PHP中的这些字符串之间插入一些文本 我用过strpos $text = "Something wordpress" if(strpos($text,'wordpress',true) !== FALSE) { //Insert <br/> text } $text=“某物wordpress” if(strpos($text,'wordpress',true)!==FALSE) { //插入文本 } 但不知道如何在字

我想在给定的字符串中搜索特定的子字符串,如果找到了,我只想在PHP中的这些字符串之间插入一些文本

我用过strpos

$text = "Something wordpress"
if(strpos($text,'wordpress',true) !== FALSE)
{
  //Insert <br/> text
}
$text=“某物wordpress”
if(strpos($text,'wordpress',true)!==FALSE)
{
//插入
文本 }
但不知道如何在字符串之间插入文本

示例输出

Something <br/> Wordpress
Something
Wordpress
$text=“Something wordpress”;
if(strpos($text,'wordpress')!==false)
{
str_replace('wordpress','
wordpress',$text); }
$text=“Something wordpress”;
if(strpos($text,'wordpress')!==false)
{
str_replace('wordpress','
wordpress',$text); }
这样就可以了

echo str_replace('wordpress', '<br>wordpress', 'Something wordpress');
echo str_替换('wordpress','
wordpress','Something wordpress');
这样就可以了

echo str_replace('wordpress', '<br>wordpress', 'Something wordpress');
echo str_替换('wordpress','
wordpress','Something wordpress');
尝试使用“str_replace”()

例如:

            $text  = "Something wordpress";
            $searchText = "wordpress";
            $replaceText = "different text";

            $newPhrase = str_replace($healthy, $replaceText, $text);
尝试使用“str_replace”()

例如:

            $text  = "Something wordpress";
            $searchText = "wordpress";
            $replaceText = "different text";

            $newPhrase = str_replace($healthy, $replaceText, $text);

我想知道为什么没有人提到插入的
substr\u replace

$text = "Something wordpress";
$pos = strpos($text,'wordpress');
if($pos !== FALSE)
{
    $text = substr_replace($text, "<br>", $pos, 0);
}
var_dump($text); // "Something <br>wordpress"
$text=“Something wordpress”;
$pos=strpos($text,'wordpress');
如果($pos!==FALSE)
{
$text=substr\u replace($text,
,$pos,0); } 变量转储($text);//“某种东西
wordpress”
我想知道为什么没有人提到插入的
substr\u replace

$text = "Something wordpress";
$pos = strpos($text,'wordpress');
if($pos !== FALSE)
{
    $text = substr_replace($text, "<br>", $pos, 0);
}
var_dump($text); // "Something <br>wordpress"
$text=“Something wordpress”;
$pos=strpos($text,'wordpress');
如果($pos!==FALSE)
{
$text=substr\u replace($text,
,$pos,0); } 变量转储($text);//“某种东西
wordpress”
您可以使用预匹配。此
preg_match
将搜索“wordpress”,并将其替换为“
wordpress”

$search = 'wordpress';
$string = preg_replace('/' . preg_quote($search, '/') . '/', '<br />$0', $string);
$search='wordpress';
$string=preg_replace(“/”.preg_quote($search“/”)“/”,“
$0”,$string);

无论多么有效,我的方法和其他答案没有任何区别。这只是一个字符串替换操作。

您可以使用preg\u匹配。此
preg_match
将搜索“wordpress”,并将其替换为“
wordpress”

$search = 'wordpress';
$string = preg_replace('/' . preg_quote($search, '/') . '/', '<br />$0', $string);
$search='wordpress';
$string=preg_replace(“/”.preg_quote($search“/”)“/”,“
$0”,$string);

无论多么有效,我的方法和其他答案没有任何区别。这只是一个字符串替换操作。

介于什么之间?提供一些例子。在某物和wordpress之间,如上面的代码所示。比如“Something
wordpress”,你的意思是说在这种情况下它是空白,对吗?我只想插入一个换行符,如果字符串之间包含“wordpress”,是什么?提供一些例子。在某物和wordpress之间,如上面的代码所示。比如“Something
wordpress”,你的意思是说在这种情况下它是空白的,对吗?我只想插入一个换行符,如果该字符串包含“wordpress”,在你允许的情况下,我可以编辑你的答案吗??只是一个小错误。如果您允许,我可以编辑您的答案吗??只是个小错误。