Talki
Talki

Reputation: 33

preg_replace image tag to other format string

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

Upvotes: 0

Views: 1022

Answers (2)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76397

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

Upvotes: 1

rr-
rr-

Reputation: 14811

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.

Upvotes: 1

Related Questions