Kapn0batai
Kapn0batai

Reputation: 215

String replace multiple values

I have a string that looks like this:

Bla bla %yada yada% bla bla %yada yada%

Is there a way to replace only the first two "%" (or the last two) so I can get the next output:

Bla bla <a href='link1'>yada yada</a> bla bla %yada yada%

and also, if necessary, the last two "%" so it outputs:

Bla bla <a href='link1'>yada yada</a> bla bla <a href='link2'>yada yada</a>

I can't figure out how to make the distinction between the first two and the last two so, if I want, I can be able to replace either the first or the last two marks "%" with a link. I'm using php. Thanks in advance

Regards

Upvotes: 3

Views: 294

Answers (2)

Navnath Godse
Navnath Godse

Reputation: 2225

Try this
Support for PHP 4 and PHP 5


Solution:

$string ='Bla bla %yada yada% bla bla %yada yada%';

// Count no of %
$count = substr_count($string,'%');

// Valid string pattern
if ( 0 == ($count % 2) ) {

    $urlString = $string;
    
    // Iterate for each pair of % to make it link
    for ( $i=1; $i <= $count/2 ; $i++  ) {
    
        $urlString = preg_replace('/%/', "<a href='link$i'>", $urlString, 1);
        $urlString = preg_replace('/%/', "</a>", $urlString, 1);
    }
}
// Invalid string pattern
else {
    echo "Invalid string pattern";
}

// Display generated link 
echo $urlString;

Working of preg_replace function

  • Remove first two %
    $str ='Bla bla %yada yada% bla bla %yada yada%';
    $newStr = preg_replace('/%/', '', $str, 2);
    
    echo $newStr;
    
    // Output => Bla bla yada yada bla bla %yada yada%

  • Remove last two %
    $str ='Bla bla %yada yada% bla bla %yada yada%';
    $newStr = preg_replace('/%/', '', strrev($str), 2);
    $newStr =  strrev($newStr);
    
    echo $newStr;
    
    // Output => Bla bla %yada yada% bla bla yada yada

  • Remove all %
    $str ='Bla bla %yada yada% bla bla %yada yada%';
    $newStr = preg_replace('/%/', '', $str);
    
    echo $newStr;
    
    // Output => Bla bla yada yada bla bla yada yada

Reference
https://www.php.net/preg%5Freplace

Upvotes: 0

HamZa
HamZa

Reputation: 14921

Using regex (PHP 5.3+ required) :

$string = 'Bla bla %yada yada% bla bla %yada yada%';
echo preg_replace('/%([^%]*)%/', '<a href="http://example.com">$1</a>', $string, 1) . '<br>'; // to replace the first instance.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
$links = array('http://example.com', 'http://stackoverflow.com', 'http://google.com');
$index = 0;
echo preg_replace_callback('/%([^%]*)%/', function($m) use($links, &$index){
    $m[1] = '<a href="'.$links[$index].'">'.$m[1].'</a>';
    $index++;
    // reset the index if it exceeds (N links - 1)
    if($index >= count($links)){
        $index = 0;
    }
    return $m[1];
}, $string).'<br>'; // to replace according to your array
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// To test with a string that contains more %% than the links
$string2 = 'Bla bla %yada yada% bla bla %yada yada% wuuut dsfsf %yada yada% sjnfsf %yada yada% jnsfds';
$links = array('http://example.com', 'http://stackoverflow.com', 'http://google.com');
$index = 0;
echo preg_replace_callback('/%([^%]*)%/', function($m) use($links, &$index){
    $m[1] = '<a href="'.$links[$index].'">'.$m[1].'</a>';
    $index++;
    // reset the index if it exceeds (N links - 1)
    if($index >= count($links)){
        $index = 0;
    }
    return $m[1];
}, $string2).'<br>'; // to replace according to your array

Online demo.

Upvotes: 3

Related Questions