0

i have to change image tags per php ...

here is the source string ...

'picture number is <img src=get_blob.php?id=77 border=0> howto use'

the result should be like this

'picture number is #77# howto use' 

I have already tested a lot, but I only get the number of the image as a result ... this is my last test ...

$content = 'picture number is <img src=get_blob.php?id=77 border=0> howto use';
$content = preg_replace('|\<img src=get_blob.php\?id=(\d+)+( border\=0\>)|e', '$1', $content);

now $content is 77

I hope someone can help me

2 Answers 2

1

Almost correct. Just drop the e flag:

$content = 'picture number is <img src=get_blob.php?id=77 border=0> howto use';
$content = preg_replace('/\<img src=get_blob.php\?id=(\d+)+( border\=0\>)/', '#$1#', $content);
echo $content;

Outputs:

picture number is #77# howto use

See documentation for more information about regular expression modifiers in PHP.

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

3 Comments

@Talki: be weary, though, your regex, as it now stands is very specific. It requires the img tag to contain a border attribute, no quotes, and a very specific url pattern (blob.php?id=). It also requires an invalid img tag (> instead of />)!.
<img> is perfectly valid in HTML (not to confuse with XML or XHTML). Ending with /> became optional from HTML5 on. stackoverflow.com/questions/7366344/…
@rr: HTML5 is still a w.i.p, and its not fully supported (yet), also XHTML isn't quite dead yet. "XHTML5" does exist. Either way, you're right, it turns out it is valid
1

Don't use the e flag, it's not necessairy for regex placeholders, just try this:

preg_replace('/\<.*\?id\=([0-9]+)[^>]*>/', '#$1#', $string);

This regex does assume id will be the first parameter of the src url, if this isn't always going to be the case, use this:

preg_replace('/\<.*[?&]id\=([0-9]+)[^>]*>/', '#$1#', $string);

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.