Jump to content

PREG_REPLACE


ckrudelux
 Share

Recommended Posts

$row = "<sys type=\"context\">";$data = array("context"=>"data", "menu"=>"data2");$row = preg_replace("/\<sys type=\"(.*)\" \/\>/", $data["$1"], $row);echo $row; //OUTPUT: data

Trying to use the value in type to select the data in the array.. but I get this error:
<b>Notice</b>: Undefined index: $1 in <b>C:\wamp\www\PM\cache.php</b> on line <b>27</b><br />
Edited by ckrudelux
Link to comment
Share on other sites

That's not the right way to use references, you should use arrays to specify each match to look for and what to replace it with:

$row = '<sys type="context">';$find = array('#<sys type="context" />#', '#<sys type="menu" />#');$replace = array('data', 'data2');$row = preg_replace($find, $replace, $row);echo $row;

Link to comment
Share on other sites

$row = "<sys type=\"context\">";$data = array("context"=>"data", "menu"=>"data2");$row = preg_replace("/\<sys type=\"(.*)\" \/\>/", $data["$1"], $row);echo $row; //OUTPUT: data
Hmm, try this:
$row = preg_replace('/<sys type="(.*)" \/>/e', 'return $data[\'$1\'];', $row);

edit: I'm not sure about the variable scope the string will get evaluated in. You might have to use $GLOBALS[\'data\'][\'$1\'] instead, it's not a very elegant solution, but it should work.

Edited by Dilated
Link to comment
Share on other sites

That's not the right way to use references, you should use arrays to specify each match to look for and what to replace it with:
$row = '<sys type="context">';$find = array('#<sys type="context" />#', '#<sys type="menu" />#');$replace = array('data', 'data2');$row = preg_replace($find, $replace, $row);echo $row;

This is whats going on and why I want to make this dynamic.. I'm loading a file with some text in it, reading one row at the time. If I find '<sys type="" />' I want the value of type to say what to replace with in the array. I can't say what the specific order each item in the array will come in either.
Link to comment
Share on other sites

Would it make more sense to just see what matched and then do a switch to decide what to do next?
Not sure what you mean but I could do a loop on the array and test ever key and see if it match but won't that process be very slow?I think it would be more logic to just use the value and I can make it to write the value out but then I try to use it to select key it stops to work.
Link to comment
Share on other sites

I mean first use preg_match to get a list of matches and then go through and replace all of those.You really only should be using regular expressions if you're looking for patterns though, not specific strings. If you want to just find and replace a bunch of static strings, just use str_replace.

$find = array('<sys type="context" />', '<sys type="menu" />');$replace = array('data', 'data2');$row = preg_replace($find, $replace, $row);

That's just like what I showed last time but it uses str_replace instead of preg, since they are static strings. That will be faster. That's the right thing to do if you're looking for a bunch of static strings. If you're looking for a general pattern instead, then the best advice I can give is to get a list of all of the matches first and then to go through them and do whatever you want for each one.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...