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 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. 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? 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. 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 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??? 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); 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. Link to comment https://forums.phpfreaks.com/topic/198001-str_replace-wildcard/#findComment-1039026 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.