/    Sign up×
Community /Pin to ProfileBookmark

Struggling with a preg_replace

What I want to do is run a function on the src of HTML image tags inside of the replace, function for the sake of argument called do_this().

[code=php]
$preg_find[] = ‘#<img([^<>].*)src=”(.*)”([^<>].*)/>#eiU’;
$preg_replace[] = “html_entity_decode(convertImage(\2))”;

$output = preg_replace($preg_find, $preg_replace, $output;
[/code]

I know the replace will bork the HTML output, I’m just trying to get it to run the function without failing on a simple version first. No matter what combination of anything I try, it’s producing a wide variety of errors. Current error for the above version is:

Parse error: syntax error, unexpected ‘:’ in functions.php(706) : regexp code on line 1

Fatal error: preg_replace(): Failed evaluating code: html_entity_decode(convertImage(image.jpg)) in /var/www/html/includes/functions.php on line 706

Very grateful for any help. ? Thank you!

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@bokehSep 05.2007 — [code=php]$preg_replace[] = "html_entity_decode(convertImage('\2'))";[/code]
Copy linkTweet thisAlerts:
@TygerTygerauthorSep 06.2007 — Oh good grief. Thank you!
Copy linkTweet thisAlerts:
@TygerTygerauthorSep 06.2007 — Erk, I'm stuck again. ? It won't run the html_entity_decode on the replace string.

[code=php]
$preg_find[] = '#<img([^<>]*)src="([^<>]*)"([^<>]*)/?>#eiU';
$preg_replace[] = "html_entity_decode(<img\1src=do_this('\2')\3/>)";
[/code]


Error:

Parse error: syntax error, unexpected '<', expecting ')' in /var/www/html/includes/functions.php(708) : regexp code on line 1

Fatal error: preg_replace(): Failed evaluating code: html_entity_decode(&lt;img src=do_this('image.gif') alt=&quot;test&quot; /&gt? in functions.php on line 708.
Copy linkTweet thisAlerts:
@bokehSep 06.2007 — When you pass a string of code to be evaled it must be parsible code. You are trying to eval this: [CODE]html_entity_decode(<img\1src=do_this('\2')\3/>)[/CODE]Can you see all the parse errors?

You need something like... (the extra concatenation is just to help you read it):
[code=php]$preg_replace[] = "html_entity_decode('<img'.'$1'.'src="'.do_this('$2').'"'.'$3'.'/>')";[/code]
×

Success!

Help @TygerTyger spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...