Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.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 用HTML标记替换字符串中的引号_Php_String - Fatal编程技术网

Php 用HTML标记替换字符串中的引号

Php 用HTML标记替换字符串中的引号,php,string,Php,String,我将以下字符串存储在PHP中的一个变量中 The words inside the quotes should be in '''bold'''. Like '''this''' and '''that''' 这里使用三个引号'表示单词应以粗体显示 用标签替换这个标签最有效的方法是什么?我想这样说: $new\u string=preg\u replace('/\'\'\'\'\'([^\']+)\'\'\'\'\'\'\'/','$1',$string)即使用户的答案是正确的,我还是使用了以

我将以下字符串存储在PHP中的一个变量中

The words inside the quotes should be in '''bold'''. Like '''this''' and '''that'''
这里使用三个引号
'
表示单词应以粗体显示

标签替换这个标签最有效的方法是什么?

我想这样说:

$new\u string=preg\u replace('/\'\'\'\'\'([^\']+)\'\'\'\'\'\'\'/','$1',$string)

即使用户的答案是正确的,我还是使用了以下函数

function makeBold($string) {
    $quote = ''''';
    $count = substr_count($string, $quote);
    for ($i = 0; $i <= $count/2; $i++) {
        $string = preg_replace("/$quote/", '<strong>', $string, 1);
        $string = preg_replace("/$quote/", '</strong>', $string, 1);
    }
    return $string;
}
函数makeBold($string){
$quote='&39;&39;&39;&39;';
$count=substr\u count($string,$quote);

对于($i=0;$i)最简单的方法之一是使用str_replace(“''”,“”,$string,1);对于@Tuim再次执行此操作,但我可能有多个单词需要加粗。
preg_replace
将帮助您。我不擅长正则表达式!!:)然后循环它,但我认为atrepp有一个更合适的答案。@Tuim str_replace()中的第四个参数用于返回替换的数量,而不是要执行的替换的数量。即使我也在考虑regex,但正在考虑是否有更好的方法。如果我没有得到任何其他更好的答案,我将接受此答案。我的解决方案唯一的问题是您的话不能包含“内部”修改regex以便选择所有字符,直到出现3个“”,而不是一个?可能如果您用其他内容替换所有“”,但问题是识别我可以用其替换的字符。
'
。如果该字符出现在单词中,则同样的问题再次出现。很好的解决方案,如果您这样做,最好使用stru替换为the count参数
$string=str_replace($quote,,$string,1);
@atrepp
str_replace()
中的count参数是一个输入参数,用于确定进行了多少次替换。它不限制进行替换的方式。