Daleeburg Posted May 21, 2007 Share Posted May 21, 2007 Is there a way in php to make it search and replace with variables, so like if i had this whatever="[text]-[number]" in a page full of code and i just wanted to change the number portion of it well leaving the text alone? ~D Quote Link to comment https://forums.phpfreaks.com/topic/52353-searching-and-replacing-with-variables/ Share on other sites More sharing options...
Lumio Posted May 21, 2007 Share Posted May 21, 2007 str_replace will help you Quote Link to comment https://forums.phpfreaks.com/topic/52353-searching-and-replacing-with-variables/#findComment-258354 Share on other sites More sharing options...
Daleeburg Posted May 21, 2007 Author Share Posted May 21, 2007 The problem is that i dont always know the number, so i need to find a way to retrieve it. Quote Link to comment https://forums.phpfreaks.com/topic/52353-searching-and-replacing-with-variables/#findComment-258358 Share on other sites More sharing options...
Lumio Posted May 21, 2007 Share Posted May 21, 2007 you mean that there is a part with text like whatever="foo-123" ? And you wnat to get to know what the number of the text is? Quote Link to comment https://forums.phpfreaks.com/topic/52353-searching-and-replacing-with-variables/#findComment-258363 Share on other sites More sharing options...
Daleeburg Posted May 21, 2007 Author Share Posted May 21, 2007 exactly and then change the number to a preset number Quote Link to comment https://forums.phpfreaks.com/topic/52353-searching-and-replacing-with-variables/#findComment-258364 Share on other sites More sharing options...
trq Posted May 21, 2007 Share Posted May 21, 2007 Take a look at preg_replace. if you get stuck with the regex ask your question in the regex board. Quote Link to comment https://forums.phpfreaks.com/topic/52353-searching-and-replacing-with-variables/#findComment-258371 Share on other sites More sharing options...
chigley Posted May 21, 2007 Share Posted May 21, 2007 <?php /* Method 1 - This method replaces ALL numbers with $replacement */ $replacement = 1; $string = "hello-123"; echo preg_replace("/(\d+)/", $replacement, $string); /* Method 2 - This method just replaces with the syntax [string]-[number] */ $replacement = 1; $string = "hello-123"; $pieces = explode("-", $string); $pieces[1] = $replacement; echo join("-", $pieces); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52353-searching-and-replacing-with-variables/#findComment-258372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.