?
Solved

regex function needed

Posted on 2006-11-23
6
Medium Priority
?
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 count occurrences of each item in an array.
Suggested Courses

777 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