4

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.


My workaround solution:
function hlStr($original)
{
    $rstr = explode('"', $original);
    return $rstr[0].'<sr>"'.$rstr[1].'"</sr>'.$rstr[2];
}
LMS
  • 4,117
  • 5
  • 27
  • 37

2 Answers2

9

In light of comments below, I think this will be a better resource for you: 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:

$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.

Community
  • 1
  • 1
Scott M.
  • 7,313
  • 30
  • 39
  • That isn't the problem. The problem is replacing the double quotes, and everything in-between (which is not pre-determined) with them, and contain them within `` tags. So `Console.WriteLine("RandomWords");` needs to turn into `Console.WriteLine("RandomWords");`. I am unsure of how to do this, because I cannot determine what "RandomWords" will be beforehand, and there could be multiple occurrences within the string that is having things replaced. – LMS Apr 06 '12 at 17:30
  • Yes, but I am unsure of how to implement this in `str_replace`, and I am unsure of how to write regex. – LMS Apr 06 '12 at 17:32
  • Plus, you have to capture the matched text to use it in the replacement. Read about "capture groups" and "backreferences". – Sebastián Grignoli Apr 06 '12 at 17:47
  • `$return_value = preg_replace('/"([^"]+)"/', '"\1"', $str);` So `[^"]+` matches any string between double quotes. The rounded brakets around them "capture" the match, so you can use it again in the replacement text with the backreference `\1`, wich means "use the first captured group. – Sebastián Grignoli Apr 06 '12 at 17:49
  • Anyway, I don't think that regex are the best way to achieve this. I would use a state machine. This text would break the above example: `Console.WriteLine("This string includes a double quote here \" and a single one here \' ");` – Sebastián Grignoli Apr 06 '12 at 17:56
  • Thanks for your help. I didn't go with your solution, but it helped me brainstorm a revised solution. Solution in main post. – LMS Apr 06 '12 at 17:58
-1

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.

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;
    }