0

I'm not an expert with Regex, but I'm trying to convert an image URL to another and delete the height and width attributes...

$content = preg_replace('/src="([^"]*)(png|jpeg|jpg|gif|bmp)"/', 'src="http://www.mysite.com/thumb.php?url=$1&width=500&height=500"', $post_content);

$content = preg_replace( '/(width|height)=\"\d*\"\s/', "", $content);

echo $content;

Echoing the results doesn't give me an image extension:

<img src="http://www.mysite.com/thumb.php?url=http://www.mysite.com/wp-content/uploads/2013/02/image.&width=500&height=500" />

How can I do this?

1
  • src\s*=\s*"([^"]+\.(png|jpeg|jpg|gif|bmp))" Commented May 3, 2013 at 22:45

1 Answer 1

2

The $1 in your replacement-string refers to the first capturing group of your regex. In other words, the value of $1 is the character sequence matched by the first (...) in the regex.

The problem is, your first set of parentheses does not include file extensions - hence the missing filename extension in the result.

Sign up to request clarification or add additional context in comments.

1 Comment

@jlafforgue: I'm glad if I could help.

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.