1

I am trying to do this regex match and replace but not able to do it.

Example

<a href=one target=home>One</a>
<a href=two>Two</a>
<a href=three target=head>Three</a>
<a href=four>Four</a>
<a href=five target=foot>Five</a>

I want to find each set of the a tags and replace with something like this

Find

<a href=one target=home>One</a>

Change to

<a href='one'>One</a>

same way the the rest of the a tags.

Any help would be very appreciated!

2
  • I'm new with regex, I don't know how to find the match to add the quotes to href. Commented May 18, 2013 at 16:09
  • 1
    Don't use regular expressions to parse HTML. You cannot reliably parse HTML with regular expressions, and you will face sorrow and frustration down the road. As soon as the HTML changes from your expectations, your code will be broken. See htmlparsing.com/php for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged. Commented May 18, 2013 at 22:52

3 Answers 3

5

Using DomDocument() would be an easier way to work with HTML.

<?php
    $str = '<a href=one target=home>One</a>
<a href=two>Two</a>
<a href=three target=head>Three</a>
<a href=four>Four</a>
<a href=five target=foot>Five</a>';
    $dom = new DomDocument();
    $dom->loadHTML($str);
    $anchors = $dom->getElementsByTagName('a');
    foreach ($anchors as $a)
    {
        if ($a->hasAttribute('target'))
        {
            $a->removeAttribute('target');
        }
    }
    $str = $dom->saveHTML();

See it in action

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

2 Comments

Thanks, but this does not add the quotes to href.
Are you sure? I see quotes in my in my fiddle. But feel free to add ` $dom->formatOutput = true;` just beforfe the last line and see if that helps.
1

Use this:

preg_replace('/<a(.*)href=(")?([a-zA-Z]+)"? ?(.*)>(.*)<\/a>/', '<a href='$3'>$5</a>', '{{your data}}');

1 Comment

Works! Thank you mightyuhu!
0

if you want a regex, try this:

$str = preg_replace('/<a [^>]*href=([^\'" ]+) ?[^>]*>/',"<a href='\1'>",$str);

I don't recommend using a regular expression to do this though.

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.