Solved

regex function needed

Posted on 2006-11-23
6
198 Views
Last Modified: 2008-03-04
Hi Experts,

I need a regex function to add a string I specify to every instance of an html tag I also specify.  The tags I would most probably use in this function is <a>, <strong> <em> & <span>

Thanks
0
Comment
Question by:nelwa
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:TeRReF
Comment Utility
Something like this?

<?php

function addString($txt, $s, $tag) {
        $tag = preg_quote($tag, '/');
        return preg_replace('/'.$tag.'/', '\0'.$s, $txt);
}

$text = 'This is a <strong></strong> word.';
$text = addString($text, 'bold', '<strong>');
print($text);

?>
0
 
LVL 1

Author Comment

by:nelwa
Comment Utility
thanks, but this doesn't work for me yet, you need to take into account that <a> or any of the other tags, won't just be the tags, but they can have attributes inside them ie: <a href="http://myurl.com" target="_blank">bleh</a>

Thanks
0
 
LVL 29

Accepted Solution

by:
TeRReF earned 500 total points
Comment Utility
How about this then:

<?php

function addString($txt, $s, $tag) {
        $tag = preg_quote($tag, '/');
        return preg_replace('/<\s*'.$tag.'[^>]*>/', '\0'.$s, $txt);
}

$text = 'This is a <strong></strong> word. This is a <a href="link"></a>';
$text = addString($text, 'bold', 'strong');
$text = addString($text, 'link', 'a');
print($text);

?>
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:nelwa
Comment Utility
ok, that works better, but it adds the text in between the tags, when it should add it within the first tag... ie:  I get <a href="bleh.com">bleh</a> and after the function I want <a href="bleh.com" rel="nofollow">bleh</a> for example...

Thanks
0
 
LVL 1

Author Comment

by:nelwa
Comment Utility
Thanks for your help, I fixed it...

function addString($txt, $s, $tag) {
  $tag = preg_quote($tag, '/');
  return stripslashes(preg_replace('/<\s*'.$tag.'/', '\0'.$s, $txt));
}
0
 
LVL 29

Expert Comment

by:TeRReF
Comment Utility
You're welcome :)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now