Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 03-02-2010, 07:51 AM regular expressions
Junior Talker

Posts: 1
Trades: 0
Hello everyone,
My name is Roi and I need your help.
Hope you will be able to help me-
I wrote a script that take variable that contain HTML code,. it replace the exact word "php" (no matter if it is contain capital letters) with the word "asp"
for example, if the variable contain:
PHP Code:
                       <a href='http://www.php.com'>myphp</abest <b>php</bwebsite <h1>PhP!!!</h1php and myphp or phpme - <u>php!</u>! 
the result will be
PHP Code:
                       <a href='http://www.asp.com'>myphp</abest <b>asp</bwebsite <h1>asp!!!</h1aspand myphp or phpme - <u>asp!</u>! 
well, the problem is that it althouth replace the letters inside the THML tags and because of that the links changed...
Here is my code:

PHP Code:
    function keepcase($word$replace) {
      
$replace[0] = (ctype_upper($word[0]) ? strtoupper($replace[0]) : $replace[0]);
      
      return 
$replace;
    }

    
$text strtolower(file_get_contents($folder.$file));
    
$replace "asp";
    
$word "php";
    
$output preg_replace('/\b' preg_quote($word) . '\b/ei'"keepcase('\\0', '$replace')"$text);
   echo 
$output



What should I change if I want the replacements to be only on the text between the HTML tags?

Thank you in advance,
Roi.
roice is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-04-2010, 08:07 AM Re: regular expressions
nayes84's Avatar
Extreme Talker

Latest Blog Post:
Difference between ASP And JSP
Posts: 232
Name: John
Location: Tokyo
Trades: 0
Well it sound a little bit hard to do but if that tag is only <b> and there is nothing inside it but php then str_replace is the best to use because str_replace is much faster than preg_replace
PHP Code:
$output=str_replace('<b>php</b>','<b>asp</b>',$text); 
Hope I could help
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

if(I'm("Helpful")) Add_Talkupation("nayes84");

Last edited by nayes84; 03-04-2010 at 08:08 AM..
nayes84 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to regular expressions
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53772 seconds with 12 queries