Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

regex function needed

Posted on 2006-11-23
6
Medium Priority
?
207 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

597 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