Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.