markyoung1984 Posted May 23, 2008 Share Posted May 23, 2008 Hi, I have the following string: <img src="test.jpg" height="200" width="300" alt="Product image"> I have been using some of the string processing functions within PHP but just can't seem to get what I want and wondered if anyone can help! I want test.jpg to be assigned to $path, 200 to be assigned to $height and 300 to be assigned to $width. The lengths of all these variables may change, e.g. height could be 1000. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/106930-string-processing/ Share on other sites More sharing options...
jaymc Posted May 23, 2008 Share Posted May 23, 2008 $filename = $_POST[filename]; $height = $_POST[height]; $width = $_POST[width]; <img src="$filename" height="$height" width="$width" alt="Product image"> use POST as an example Quote Link to comment https://forums.phpfreaks.com/topic/106930-string-processing/#findComment-548038 Share on other sites More sharing options...
Haggis Posted May 23, 2008 Share Posted May 23, 2008 dont know if this would help you <?PHP $data = "<img src='test.jpg' height='200' width='300' alt='Product image'>"; $pieces = explode("'", $data); echo "Image name: " . $pieces[1]; echo "<br>"; echo "Width: " . $pieces[3]; echo "<br>"; echo "Height: " . $pieces[5]; echo "<br>"; echo "Alt Tag: " . $pieces[7]; echo "<br>"; ?> This outputs Image name: test.jpg Height: 200 width: 300 Alt Tag: Product image Quote Link to comment https://forums.phpfreaks.com/topic/106930-string-processing/#findComment-548041 Share on other sites More sharing options...
vurentjie Posted May 23, 2008 Share Posted May 23, 2008 I don't know where you getting $width $height $path from, but I suspect you want to input them somewhere and resize the image accordingly, in that case, as mention above use POST or GET, then retrieve these values and run some conditional: if(isset($_GET['width'])){$width = $_GET['width'];}else{$width = "300px";} if(isset($_GET['height])){$height = $_GET['height'];}else{$height = "200px";} if(isset($_GET['path'])){$path = $_GET['path'];}else{$path = "test.jpg";} echo "<img src='".$path."' width='".$width."' height='".$height."' />"; there is probably better ways to do this, but this is one of quite a few, remember to use quotations wisely, hope it helps Quote Link to comment https://forums.phpfreaks.com/topic/106930-string-processing/#findComment-548048 Share on other sites More sharing options...
sasa Posted May 23, 2008 Share Posted May 23, 2008 try <?php $string = '<img src="test.jpg" height="200" width="300" alt="Product image">'; preg_match_all('/(\S+)="([^"]+)"/', $string, $out); $out = array_combine($out[1], $out[2]); print_r($out); ?> Quote Link to comment https://forums.phpfreaks.com/topic/106930-string-processing/#findComment-548132 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.