StefanRSA Posted April 8, 2010 Share Posted April 8, 2010 I have a string that contains sizes width="xxx" height="yyy" How can I use str_replace to change all of them to width="400" height="319" if all the sizes differ? I cannot use: $object=$_POST['objectdetail']; $object=str_replace('%body%', '400', 'width="%body%"'); $object=str_replace('%bodya%', '319', 'height="%bodya%"'); Please help Quote Link to comment https://forums.phpfreaks.com/topic/198001-str_replace-wildcard/ Share on other sites More sharing options...
Maq Posted April 8, 2010 Share Posted April 8, 2010 You need to use regex, have a look at preg_replace. Quote Link to comment https://forums.phpfreaks.com/topic/198001-str_replace-wildcard/#findComment-1038979 Share on other sites More sharing options...
StefanRSA Posted April 8, 2010 Author Share Posted April 8, 2010 Thanks for the fast reply.... I tried : $reg = '#width="[^"]*" height="[^"]*">#'; $rep='width="400" height="319"'; $object=preg_replace($reg,$rep,$object); Why is this not working? Quote Link to comment https://forums.phpfreaks.com/topic/198001-str_replace-wildcard/#findComment-1038981 Share on other sites More sharing options...
Maq Posted April 8, 2010 Share Posted April 8, 2010 Try: $reg = '#width="[0-9]+" height="[0-9]+">#/i'; This means, "match one or more digits inside the quotes" and the /i is for case insensitivity. Before you were using: [^"]* which doesn't really do anything. It literally translates to, "match zero or more not quotes". You may even get an error. Quote Link to comment https://forums.phpfreaks.com/topic/198001-str_replace-wildcard/#findComment-1038992 Share on other sites More sharing options...
StefanRSA Posted April 8, 2010 Author Share Posted April 8, 2010 Great! Thanks Maq, will try this Quote Link to comment https://forums.phpfreaks.com/topic/198001-str_replace-wildcard/#findComment-1039001 Share on other sites More sharing options...
StefanRSA Posted April 8, 2010 Author Share Posted April 8, 2010 I tried it now as follow: $object=trim($_POST['object']); $reg = '#width="[0-9]+" height="[0-9]+"#/i'; $rep='width="400" height="319"'; $object=preg_replace($reg,$rep,$object); error_reporting(E_ALL); echo $object; And nothing happens... My string is now gone??? Quote Link to comment https://forums.phpfreaks.com/topic/198001-str_replace-wildcard/#findComment-1039015 Share on other sites More sharing options...
StefanRSA Posted April 8, 2010 Author Share Posted April 8, 2010 I am now doing an echo before the preg_replace and after.... Only the first echo prints, the second does not? Any idea why? $object=trim($_POST['object']); echo $object; $reg = '#width="[0-9]+" height="[0-9]+"#/i'; $rep='width="400" height="319"'; $object=preg_replace($reg,$rep,$object); echo $object; error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/198001-str_replace-wildcard/#findComment-1039021 Share on other sites More sharing options...
Maq Posted April 8, 2010 Share Posted April 8, 2010 Sorry, my code was erroneous. Try this: $object=trim($_POST['object']); echo $object; $reg = '#width="[0-9]+" height="[0-9]+"#i'; $rep='width="400" height="319"'; $object=preg_replace($reg,$rep,$object); echo $object; Took out the '/' from the regex. We don't need it because we're using the '#' as delimiters. Also, when you're displaying error_reporting you should put it at the top, because anything above it will not be parsed. Quote Link to comment https://forums.phpfreaks.com/topic/198001-str_replace-wildcard/#findComment-1039026 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.