Jump to content

Manipulating a variable.


comeran

Recommended Posts

I have a variable that comes out with a full HTML img tag.

I need to remove all the text before the img name, and all the text after so the only thing left is the actual name of the image.

EXAMPLE:  <img src="%%URLofImages%%/WHATINEED.JPG" style="border: 0px">
so I need to remove the "<img src="%%URLofImages%%/" and the "" style="border: 0px">" from the string to leave WHATINEED.JPG.

I have looked up variable manipulation, variable editing, variable text stripping and have found nothing that fits what I am looking to do.

Thanks for all the help.

Com-
Link to comment
Share on other sites

After reading this post I noticed that I might not have been too clear on what I am tring to do  ::)

Right now I have:
    $variable = <img src="%%URLofImages%%/WHATINEED.JPG" style="border: 0px">;

and I need it to be:
    $variable = WHATINEED.JPG;

-----------------

I could have done this in PERL using

$variable = <img src="%%URLofImages%%/WHATINEED.JPG" style="border: 0px">;
$variable =~ s/<img src=\"%%URLofImages%%\///g;
$variable =~ s/\" style=\"border: 0px\">//g;

I just can't figure out how it's done in PHP.

Thanks again for the help.
Link to comment
Share on other sites

Ok I think that I have it but if anyone here thinks I am doing this all wrong I am open to suggestions :p

For others w/ the same problem you want to see the preg_replace function in the manual.
[url=http://us3.php.net/manual/en/function.preg-replace.php]http://us3.php.net/manual/en/function.preg-replace.php[/url]

[b]I HAD[/b]
    $imagename = $data['image'];
which came out like:
    $imagename = <img src="%%URLofImages%%/WHATINEED.JPG" style="border: 0px">;
and all I wanted was "WHATINEED.JPG" so I needed to remove text before and after that text.

[b]THIS IS HOW I DID IT[/b]
    $imagetemp = $data['image'];
    $patterns[0] = '/<img src=\"%%URLofImages%%\//';
    $patterns[1] = '/\" style=\"border: 0px\">/';
    $replacements[1] = '';
    $replacements[0] = '';
    $imagename = preg_replace($patterns, $replacements, $imagetemp);


There could be a better way, and if anyone here knows then please feel free I just wanted to post the answer that I had in case it would help others in my spot.

Thanks for the great board, I will be using this often on my journal to learn PHP.

Com-
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.