Solved

regex function needed

Posted on 2006-11-23
6
199 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
ID: 18006278
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
ID: 18006314
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
ID: 18006337
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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 1

Author Comment

by:nelwa
ID: 18006368
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
ID: 18006384
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
ID: 18006425
You're welcome :)
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to dynamically set the form action using jQuery.

770 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