Jump to content

str_replace using wildcards


gazever

Recommended Posts

Hi Can anyone help,

 

I have just spent about an hour googling str replace using wildcards to no joy.

 

Can someone suggest how i would go about replacing: "(3,6) (9)"

 

within a string to: "(3, 6)"

 

however I need to use wildcards for the numbers as these could be "(2,4) (6)" for instance

 

this searching goes thorugh an array containing crossword clues, so not every string will have these specifics, many will just have the number, i.e "(9)"

 

Hope this makes sense!

 

Many Thanks

 

Gazever

Link to comment
https://forums.phpfreaks.com/topic/53400-str_replace-using-wildcards/
Share on other sites

@thorpe,

 

yup worked a treat, thanks

 

had to replace the brackets first as these interferred with the preg code i think, so changed them before then reverted them back.

 

 

<?php
////// replace --->( and --->) to not mess up the pregreplace
foreach ($newpuzzle as &$value) {
  $value = str_replace("(", "LEFTBRACKET", $value);
  $value = str_replace(")", "RIGHTBRACKET", $value);
}


////// replace "(?,?) (?)" with "(?, ?)" 
foreach ($newpuzzle as &$value) {
$old = '/(LEFTBRACKET)(.*)(,)(.*)(RIGHTBRACKET LEFTBRACKET)(.*)(RIGHTBRACKET)/';
$new = 'LEFTBRACKET${2}${3} ${4}RIGHTBRACKET';
  $newvalue = preg_replace($old, $new, $value);
$value = $newvalue;
}
?>

 

@svivian,

 

Thanks but already sorted the solution from Thorpes suggestion.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.