-2

hello I have the following tags :

$content ='<a href="http://website.com/" />
    <a href="/link1" />
    <a href="https://website.com" />
    <a href="link1" />';

and this code :

preg_replace('~href=(\'|"|)(.*?)(\'|"|)(?<!\/|http:\/\/|https:\/\/)~i',  'href=$1http://website2.com/$2$3', $content);

I want to use the code above to replace href tags doesn't start with an http or https or with a slash .
thanks in advance.

1 Answers1

0

Something like this should do it. I'd advise using a parser in the future, How do you parse and process HTML/XML in PHP?, for tasks such as this though. This can become very messy quickly. Here's a link on this regex usage as well, http://www.rexegg.com/regex-best-trick.html.

Regex:

/href=("|')https?:\/\/(*SKIP)(*FAIL)|href=("|')(.*?)\2/

Demo: https://regex101.com/r/cV2xB5/1

PHP Usage:

$content ='<a href="http://website.com/" />
    <a href="/link1" />
    <a href="https://website.com" />
    <a href="link1" />';
echo preg_replace('/href=("|\')https?:\/\/(*SKIP)(*FAIL)|href=("|\')\/?(.*?)\2/', 'href=$2http://website2.com/$3$2', $content);

Output:

<a href="http://website.com/" />
    <a href="http://website2.com/link1" />
    <a href="https://website.com" />
    <a href="http://website2.com/link1" />

Update, for // exclusion

Use:

href=("|')(?:https?:)?\/\/(*SKIP)(*FAIL)|href=("|')(.*?)\2

Demo: https://regex101.com/r/cV2xB5/2

PHP:

$content ='<a href="//website.com/" />
    <a href="/link1" />
    <a href="https://website.com" />
    <a href="link1" />';
echo preg_replace('/href=("|\')(?:https?:)?\/\/(*SKIP)(*FAIL)|href=("|\')\/?(.*?)\2/', 'href=$2http://website2.com/$3$2', $content);

Output:

<a href="//website.com/" />
    <a href="http://website2.com/link1" />
    <a href="https://website.com" />
    <a href="http://website2.com/link1" />
Community
  • 1
  • 1
chris85
  • 23,846
  • 7
  • 34
  • 51
  • thanks for this , and could you please tell me how can I use it for // too – James sherwod Jul 29 '15 at 01:00
  • if href starts with // then don't replace it – James sherwod Jul 29 '15 at 01:00
  • Updated for `//` exclusion as well. – chris85 Jul 29 '15 at 01:19
  • With update no longer accepted answer? Something wrong with update? – chris85 Jul 29 '15 at 01:20
  • now my code works fine for all href as I wanted , but one href who starts with one slash like this – James sherwod Jul 29 '15 at 11:14
  • don't get replaced , and yes this is good this is what I want now I want to replace href who starts with one slash with another link let's say http://website3.com/ – James sherwod Jul 29 '15 at 11:14
  • You can't do that with this expression. You will need another expression. You could update this expression so if it starts with one `/` it doesn't get replaced. I'll leave that to you though, you should have all the information you need here. This answer answers your original question, and your additional question (2 questions with one answer!). You seem to be approaching http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem. – chris85 Jul 29 '15 at 13:55
  • ok @chris85 you helped me so much I gave you the accepted answer :) – James sherwod Jul 29 '15 at 14:43