1

I'm trying to manipulate a path to an image using php. The path has a variable userdirectory in it, so i'm struggling with how to write this.

Ive used to do :

$texthtml = $content;
if (preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $texthtml, $image) ) {
$promopic = $image['src'];
}

to find if there is an image in my content and make a variable out of it. that works, but i need to alter my code to load image from a thumbnail directory for pageload reasons.

an image src looks like this : "/uploads/userdirs/admin(variable)/image.jpg"

But i want it to be this :

"/uploads/userdirs/admin(variable)/mcith/mcith_image.jpg"

adding a /mcith/ and a mcith_prefix to the image. I'm thinking exploding it, but i dont know how to do that with a variable path. Any pointers greatly appreciated!

5
  • Obligatory can't parse HTML with regex comment. Commented Apr 20, 2012 at 12:52
  • @Dotmister Please avoid comments like this. They serve only to create noise. Commented Apr 20, 2012 at 12:53
  • @Havihavi Do you want to explode (meaning break into parts resulting in an array) the string, or merely replace some of its content to result in a new string? Commented Apr 20, 2012 at 12:54
  • i thought of using str_replace or explode to get a new string with the desired value. but i wouldnt know how to, as the /admin/ path is variable Commented Apr 20, 2012 at 12:57
  • check out parse_url, ca3.php.net/parse_url Commented Apr 20, 2012 at 13:00

2 Answers 2

1

You can do this many different ways, but I would probably use pathinfo() for this:

$path = pathinfo($promopic);
$thumb = $path['dirname'] . '/mcith/mcith_' . $path['basename'];
Sign up to request clarification or add additional context in comments.

1 Comment

Worked great! i gotta read up on pathinfo. awesome stuff, thank you =)
0

very simple version, could use improvement

preg_replace('/\/([a-zA-Z0-9]+\.[a-zA-Z0-9]{3,4})$/', '/mcith/mcith_$1', '/uploads/userdirs/admin(variable)/image.jpg');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.