Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.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 - Fatal编程技术网

如何在php中用给定字符串中的给定替换项替换所有出现的特定单词

如何在php中用给定字符串中的给定替换项替换所有出现的特定单词,php,Php,请大家注意,我只想让用户伪装成html,因此要编写链接,他们只需编写以下内容: #link webpage.php #linktoken #linktext Click here linktext# link# 这相当于编写以下html: <a href="webpage.php">Click here</a> 有解决办法吗?提前感谢。首先,我要说的是,我对这将为您的用户提供的体验感到很惭愧。哪位用户只想通过这个复杂的过程来输入一个超链接 除此之外

请大家注意,我只想让用户伪装成html,因此要编写链接,他们只需编写以下内容:

#link webpage.php #linktoken #linktext Click here linktext# link#
这相当于编写以下html:

<a href="webpage.php">Click here</a>

有解决办法吗?提前感谢。

首先,我要说的是,我对这将为您的用户提供的体验感到很惭愧。哪位用户只想通过这个复杂的过程来输入一个超链接

除此之外,还有三种方法:

代码:()

$translations=['#link'=>'];
$string='此处有一个测试:#link webpage1.php#linktoken#linktext单击此处1 linktext#link#第二个测试:#link webpage2.php#linktoken#linktext单击此处2 linktext#link#;
echo strtr($string,$translations);
回显“\n\n”;
echo str_replace(数组_键($translations),$translations,$string);
回显“\n\n”;
echo preg#u replace('/#link(.*)linktoken#linktext(.*)linktext#link#/','$string);
输出:

Here is a test: <a href="webpage1.php">Click here 1</a> and a second: <a href="webpage2.php">Click here 2</a>

Here is a test: <a href="webpage1.php">Click here 1</a> and a second: <a href="webpage2.php">Click here 2</a>

Here is a test: <a href="webpage1.php">Click here 1</a> and a second: <a href="webpage2.php">Click here 2</a>
这里有一个测试:还有第二个:
这是一个测试:第二个:
这是一个测试:第二个:

您需要将
字符串
拆分为
数组
对于
数组中的每个
项,您需要检查该项是否为关键字,如果是,则对其进行
修剪
并替换为相应的替换项

1。拆分:

$strArray = explode(" ", $input);
$output = "";
foreach ($strArray as $key => $value) {
    if ((strpos($value, "#") !== false) && (strpos($value, "link") !== false)) {
        $val = trim($value);
        if ($val === "#link") $output .= '<a href="';
        else if ($val === "#linktoken") $output .= ">";
        else if (($val === "#linktext") || ($val === "linktext#")) $output .= "";
        else if ($val === "link#") $output .= "</a>";
    } else {
        $output .= ((strlen($output) === 0) ? " " : "") . $value;
    }
}
2。检查关键字:

$strArray = explode(" ", $input);
$output = "";
foreach ($strArray as $key => $value) {
    if ((strpos($value, "#") !== false) && (strpos($value, "link") !== false)) {
        $val = trim($value);
        if ($val === "#link") $output .= '<a href="';
        else if ($val === "#linktoken") $output .= ">";
        else if (($val === "#linktext") || ($val === "linktext#")) $output .= "";
        else if ($val === "link#") $output .= "</a>";
    } else {
        $output .= ((strlen($output) === 0) ? " " : "") . $value;
    }
}
$output=”“;
foreach($key=>$value){
if((strpos($value,“#”)!==false)和&(strpos($value,link”)!==false)){
$val=修剪($value);
如果($val==“#link”)$output.='”;
}否则{
$output.=((strlen($output)==0)?“”:“”.$value;
}
}

我会尽量避免问“为什么”。你试过什么?你试过了吗?或者。这两种方法都是合适的解决方案(preg_replace()可能更好)。你有什么问题?使用
trim(str)
要删除空白,如果我说谢谢@mickmackusa,这仍然是轻描淡写的。这正是我想要的。非常感谢。要重新打开此页面,您需要编辑/改进您的问题并显示您的编码尝试。也感谢@Lajos Arpad的帮助,但是,最后一行代码给出了以下错误:致命错误:未捕获错误:函数名必须是…行中的字符串…请您解决此问题。@smacaz感谢您指出,这是一个拼写错误,我已编写$strlen而不是strlen。我已编辑了我的答案以解决此问题。