1

Unfortunately I really cannot get my head around regular expressions so my last resort is to ask the help of you fine people.

I have this existing code:

<li id="id-21" class="listClass" data-author="newbie">
    <div class="someDiv">
        <span class="spanClass">Some content</span>
    </div>

    <div class="controls faint">
        <a href="link2">Link 2</a>
        <a href="link3">Link 3</a>
    </div>
</li>

Due to a number of reasons, I have to use preg_replace to inject an additional piece of code:

<a href="link1">Link 1</a>

I think you can guess where that should go, but for the sake of clarity, my desire is for the resulting string to look like:

<li id="id-21" class="listClass" data-author="newbie">
    <div class="someDiv">
        <span class="spanClass">Some content</span>
    </div>

    <div class="controls faint">
        <a href="link1">Link 1</a>
        <a href="link2">Link 2</a>
        <a href="link3">Link 3</a>
    </div>
</li>

Can anyone help me with the appropriate regular expression to achieve this?

6
  • You'd better more precise. Do you mean you just want to insert <a href="link1">Link 1</a> in front of <a href="link2">Link 2</a> ??? Commented Nov 13, 2012 at 13:18
  • 4
    Don't use regex to parse/manipulate HTML. Get a proper HTML parser. Commented Nov 13, 2012 at 13:18
  • @Alvin Wong True, Regex cannot parse arbitrary HTML. period. Commented Nov 13, 2012 at 13:19
  • @luiges90 Yes, that's what I want to do. Commented Nov 13, 2012 at 13:25
  • I'm manipulating HTML templates that are later compiled and parsed in the software that I am using. Don't get too hung up on the HTML side of things. If my example wasn't HTML and was just an arbitrary string, how would I achieve this with regex? Commented Nov 13, 2012 at 13:27

2 Answers 2

2

try this

$html = '<li id="id-21" class="listClass" data-author="newbie">
    <div class="someDiv">
        <span class="spanClass">Some content</span>
    </div>

    <div class="controls faint">
        <a href="link2">Link 2</a> 
        <a href="link3">Link 3</a>
    </div>
</li>';


$eleName = 'a';
$eleAttr = 'href';
$eleAttrValue = 'link2';
$addBefore = '<a href="link1">Link 1</a>';

$result = regexAddBefore($html, $eleName, $eleAttr, $eleAttrValue, $addBefore);

var_dump($result);

function regexAddBefore($subject, $eleName, $eleAttr, $eleAttrValue, $addBefore){
    $regex = "/(<\s*".$eleName."[^>]*".$eleAttr."\s*=\s*(\"|\')?\s*".$eleAttrValue."\s*(\"|\')?[^>]*>)/s";
    $replace = $addBefore."\r\n$1";

    $subject = preg_replace($regex, $replace, $subject);

    return $subject;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I can suggest two things (Although I couldn't understand your problem clearly)

$newStr = preg_replace ('/<[^>]*>/', ' ', $htmlText); 

this will remove all the html tags from the string. I don't know if it will be usefull for you.

Another recommendation would be to use strip_tags function. The second parameter of strip_tags is optional. You can define the tags you want to keep with the help of 2nd parameter.

$str = '<li id="id-21" class="listClass" data-author="newbie">
        <div class="someDiv">
            <span class="spanClass">Some content</span>
        </div>

        <div class="controls faint">
            <a href="link2">Link 2</a>
            <a href="link3">Link 3</a>
        </div>
        </li>';

echo strip_tags ($str,'<a>');

This will give you an output just with the links and whatever text in the html string.

Sorry if this also doesn't help.

2 Comments

The software I'm working with doesn't work like that, unfortunately. Your solution would definitely work in normal cases but essentially I'm working to create a plugin and manual template edits aren't workable. The big chunk of code is the raw template code that I can pull out of the database and my only option is to work out some regex to inject the <a href="link1">Link 1</a> code into that. The software later parses it and does whatever it needs to do to make the HTML work. I just need to get that link in there. Thank you so much for taking the time to reply.
ok then see the new answer. Is that what you are searching for? I tried to understand the problem but may be I am not good with it.

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.