Solved

regex function needed

Posted on 2006-11-23
6
200 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 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How would I construct this INSERT statement in PDO? 6 33
MySQL-Design Help 12 44
A responsive image gallery using flexbox 6 24
How to remove  "0":  in a json string 4 18
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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.

730 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