Jump to content

String processing


markyoung1984

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.