/    Sign up×
Community /Pin to ProfileBookmark

Is this preg_replace correct ?

Hi,

The HTML5 validator doesn’t like ‘&’ in the urls it wants ‘&’
so I am trying to change them before they get output from the server.

I can not stop users from pasting if urls with ‘&’ in them so I just want to swap them out.

Because I only want to change ‘&’ that are in urls – not anywhere else
I need to match a pattern.

So I have my preg_replace like this:

$text= preg_replace(“/[href(.+?)][&](.+?)/s”,’href’.$1.’&’.$2,$text);

Dunno why – just doesn’t look right 😮

Hopefully I will get the hang of this one day !! ?

Thanks.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@unasAquilaDec 21.2013 — wouldn't str_replace Be a lot easier?

[code=php]
str_replace('&', '&', 'http://www.example.com/search.php?query=Hello&world&this&is&a&test');
[/code]
Copy linkTweet thisAlerts:
@chrisranjanaDec 21.2013 — 
Dunno why - just doesn't look right :o


Hopefully I will get the hang of this one day !! ?


Thanks.[/QUOTE]


You could test your regular expression online using testers like http://gskinner.com/RegExr/
×

Success!

Help @jeddik 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.15,
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,
)...