php - 替换双引号内的内容,包括双引号

时间:2022-09-15 16:21:18

So I decided to have a stab at making a text highlighting system. At the moment, I'm just using str_replace to replace a word (e.g. $rstr = str_replace("Console", "<c>Console</c>", $str where $str is the input string.

Something that stumped me was how to replace content inside of speech marks (") and quotes ('). For example, if the string "Console" turned into Console.WriteLine("Words");, how would I replace "Words" with <sr>"Words"</sr> (<sr> is defined in an external stylesheet)?

I had a though that I could use regex, but 1. I don't know how to write regex, and 2. I don't know how to use regex with str_replace.

所以我决定尝试制作一个文本突出显示系统。目前,我只是使用str_replace来替换一个单词(例如$ rstr = str_replace(“Console”,“ Console ”,$ str,其中$ str是输入字符串。有些东西让我难过是如何替换语音标记(“)和引号(')内的内容。例如,如果字符串”Console“变成Console.WriteLine(”Words“);,我如何用 替换”Words“ “单词” ( 是在外部样式表中定义的)?我有一个虽然我可以使用正则表达式,但是1.我不知道怎么写正则表达式,而且2.我不知道如何在str_replace中使用正则表达式。


My workaround solution:

function hlStr($original)
{
    $rstr = explode('"', $original);
    return $rstr[0].'<sr>"'.$rstr[1].'"</sr>'.$rstr[2];
}

2 个解决方案

#1


7  

In light of comments below, I think this will be a better resource for you: http://www.regular-expressions.info/

根据以下评论,我认为这将是一个更好的资源:http://www.regular-expressions.info/

In order to find "anything can go here" you should use regular expressions. This is what they were made for. A regular expression for that might look something like the answers in this question:

为了找到“任何可以去的地方”你应该使用正则表达式。这就是他们的目的。对此的正则表达式可能类似于此问题中的答案:

How can I match a quote-delimited string with a regex?

如何将引号分隔的字符串与正则表达式匹配?

then you would use the function preg_replace() like this:

然后你会像这样使用preg_replace()函数:

$return_value = preg_replace('/"[^"]+"/', 'replacement text', $str)

leaving this here anyway:

无论如何离开这里:

just escape the content with a backslash:

用反斜杠逃避内容:

$rstr = str_replace("Console", "Console.WriteLine(\"$variable\");", $str)

this is mostly useful if you are using variables inside your strings. If it is just a straight text replacement, use single quotes:

如果您在字符串中使用变量,这将非常有用。如果它只是一个直接文本替换,请使用单引号:

$rstr = str_replace("Console", 'Console.WriteLine("Words");', $str)

the single quotes count everything but single quotes as just a character.

单引号计算所有内容,但单引号只是一个字符。

#2


0  

This is my solution. Explode the whole string by ( " ) symbols, and then run a specific code to each second of them. This code does automatic do it to every second value after " item, which means, if you does : hej " lol ; it would change to : hi <sr>" lol "</sr> ; or if you do : hi " with " you ; it would change to : hi <sr>" with "</sr> you ; etc.

这是我的解决方案。用(“)符号分解整个字符串,然后对每一个字符串运行一个特定的代码。这个代码自动执行”item“之后的每一秒值,这意味着,如果你这样做:hej”lol;它会改变to:hi “lol” ;或者如果你这样做:hi“with”yo​​u;它会改为:hi “with” you; etc.

function wrapInside($text,$symbol)
    {
        $string = explode($symbol, $text);
        $i = 1;
        $QS = '';
        foreach( $queryString as $V )
        {
            ( $i == 1 ) ? ( $QS .= $V ) : ( $QS .= '<sr>"'.trim($V).'"</sr>' );
            ( $i == 1 ) ? ( $i = 0 ) : ( $i = 1 );
        }
        $queryString = trim($QS);
        return $queryString;
    }

#1


7  

In light of comments below, I think this will be a better resource for you: http://www.regular-expressions.info/

根据以下评论,我认为这将是一个更好的资源:http://www.regular-expressions.info/

In order to find "anything can go here" you should use regular expressions. This is what they were made for. A regular expression for that might look something like the answers in this question:

为了找到“任何可以去的地方”你应该使用正则表达式。这就是他们的目的。对此的正则表达式可能类似于此问题中的答案:

How can I match a quote-delimited string with a regex?

如何将引号分隔的字符串与正则表达式匹配?

then you would use the function preg_replace() like this:

然后你会像这样使用preg_replace()函数:

$return_value = preg_replace('/"[^"]+"/', 'replacement text', $str)

leaving this here anyway:

无论如何离开这里:

just escape the content with a backslash:

用反斜杠逃避内容:

$rstr = str_replace("Console", "Console.WriteLine(\"$variable\");", $str)

this is mostly useful if you are using variables inside your strings. If it is just a straight text replacement, use single quotes:

如果您在字符串中使用变量,这将非常有用。如果它只是一个直接文本替换,请使用单引号:

$rstr = str_replace("Console", 'Console.WriteLine("Words");', $str)

the single quotes count everything but single quotes as just a character.

单引号计算所有内容,但单引号只是一个字符。

#2


0  

This is my solution. Explode the whole string by ( " ) symbols, and then run a specific code to each second of them. This code does automatic do it to every second value after " item, which means, if you does : hej " lol ; it would change to : hi <sr>" lol "</sr> ; or if you do : hi " with " you ; it would change to : hi <sr>" with "</sr> you ; etc.

这是我的解决方案。用(“)符号分解整个字符串,然后对每一个字符串运行一个特定的代码。这个代码自动执行”item“之后的每一秒值,这意味着,如果你这样做:hej”lol;它会改变to:hi “lol” ;或者如果你这样做:hi“with”yo​​u;它会改为:hi “with” you; etc.

function wrapInside($text,$symbol)
    {
        $string = explode($symbol, $text);
        $i = 1;
        $QS = '';
        foreach( $queryString as $V )
        {
            ( $i == 1 ) ? ( $QS .= $V ) : ( $QS .= '<sr>"'.trim($V).'"</sr>' );
            ( $i == 1 ) ? ( $i = 0 ) : ( $i = 1 );
        }
        $queryString = trim($QS);
        return $queryString;
    }