Viewed   74 times

I'm trying to replace parts of my string. But I met a problem when my search string start with same character:

$string = "Good one :y. Keep going :y2"; 

$str = str_replace(array_keys($my_array), array_values($my_array), $string);   
$my_array= array(":y" => "a", ":y2" => "b");


Good one a. Keep going a2

I need my str_replace() to match the word correctly/exactly.



Besides that you should define your array first before you use it, this should work for you:

$str = strtr($string, $my_array);

Your problem is that str_replace() goes through the entire string and replaces everything it can, you can also see this in the manual.

And a quote from there:

Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document.

So for this I used strtr() here, because it tries to match the longest byte in the search first.

You can also read this in the manual and a quote from there:

If given two arguments, the second should be an array in the form array('from' => 'to', ...). The return value is a string where all the occurrences of the array keys have been replaced by the corresponding values. The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.

Wednesday, October 19, 2022

I think you aren't looking for a regex, but for the stripslashes($str) method.

EDIT: From the comments, I understand that you will only replace " with nothing, you should use a simple str_replace here, as @Gumbo said:

$str = ...;
$newStr = str_replace('"', '', $str);

echo $newStr;

You can use regular expressions for this, but the pReg library isn't fast, if you can find a str_* or array variant which does the same I always recommend to use that instead of preg_*

Friday, October 7, 2022

It's safer to parse HTML with DOMDocument instead of regex. Try this code:


function replaceInAnchors($html)
    $dom = new DOMDocument();
    // loadHtml() needs mb_convert_encoding() to work well with UTF-8 encoding
    $dom->loadHtml(mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"));

    $xpath = new DOMXPath($dom);

    foreach($xpath->query('//text()[(ancestor::a)]') as $node)
        $replaced = str_ireplace('_', ' ', $node->wholeText);
        $newNode  = $dom->createDocumentFragment();
        $node->parentNode->replaceChild($newNode, $node);

    // get only the body tag with its contents, then trim the body tag itself to get only the original content
    return mb_substr($dom->saveXML($xpath->query('//body')->item(0)), 6, -7, "UTF-8");

$html = '<li class="hook">
      <a href="i_have_underscores">I_have_underscores</a>
echo replaceInAnchors($html);
Monday, September 19, 2022

Use gsub

dat <- c("test", "testing", "esten", "etsen", "blest", "estten")

gsub("t", "", dat)
[1] "es"    "esing" "esen"  "esen"  "bles"  "esen" 
Sunday, August 14, 2022

It's possible with Perl:

my $string = "===== Hello World ====";
$string =~ s/(====+)/"-" x length($1)/eg;
# $string contains ----- Hello World ----

Flag /e makes Perl execute expression in second part of s///. You may try this with oneliner:

perl -e '$ARGV[0] =~ s/(====+)/"-" x length($1)/eg; print $ARGV[0]' "===== Hello World ===="
Wednesday, September 14, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :