PHP Token replaces html entities - Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Webmaster Tech > Programming > PHP Development

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-15-2011, 04:28 PM   #1 (permalink)
Junior Member
 
Join Date: Jul 2010
Posts: 1
Default PHP Token replaces html entities

I want to make certain words/strings like links if found in the text. I have a piece of code from php.bet which does that, but it also removes the beginning and end of tags from <img src="my_image.jpg" />. Can you help solve this?

Here's the piece of code:

PHP Code:
<?php

$str_in 
=   '<p>Hi there worm! You have a disease!</p><img src="my_image.jpg" />
            '
;
$replaces=      array(
                
'worm' => 'worm.html',
                
'disease' => 'disease.html'
                
);

function 
addLinks($str_in$replaces)
{
  
$str_out '';
  
$tok strtok($str_in'<>');
  
$must_replace = (substr($str_in01) !== '<');
  while (
$tok !== false) {
    if (
$must_replace) {
      foreach (
$replaces as $tag => $href) {
        if (
preg_match('/\b' $tag '\b/i'$tok)) {
          
$tok preg_replace(
                                
'/\b(' $tag ')\b/i',
                                
'<a title="' $tag '" href="' $href '">\1</a>',
                                
$tok,
                                
1);
          unset(
$replaces[$tag]);
        }
      }
    } else {
      
$tok "<$tok>";
    }
    
$str_out .= $tok;
    
$tok strtok('<>');
    
$must_replace = !$must_replace;
  }
  return 
$str_out;
}

echo 
addLinks($str_in$replaces);
The result is:

PHP Code:
        Hi there wormYou have a disease!
    
    
img src="my_image.jpg" /< > 
The "worm" and "disease" words are transformed into links like desired, but the rest...

Thanks a lot!
cicovy is offline   Reply With Quote
Sponsored Links
Old 11-25-2011, 10:49 AM   #2 (permalink)
Regular User
 
Join Date: Sep 2010
Posts: 30
Default Re: PHP Token replaces html entities

Try this,
PHP Code:
function addLinks($str_in$replaces

  
$str_out ''
  
$tok strtok($str_in'<>'); 
  
$must_replace = (substr($str_in01) !== '<'); 
  while (
$tok !== false
  {   
    if (
$must_replace) {     
      foreach (
$replaces as $tag => $href) { 
        if (
preg_match('/\b' $tag '\b/i'$tok)) { 
          
$tok preg_replace(  '/\b(' $tag ')\b/i'
                                
'<a title="' $tag '" href="' $href '">\1</a>'
                                
$tok
                                
1); 
          unset(
$replaces[$tag]); 
        } 
      } 
    } 
    if(
trim($tok)!="")
    {
    
//else { 
      
$tok "<$tok>"
    
//}
    

    
$str_out .= $tok
    
$tok strtok('<>'); 
    
$must_replace = !$must_replace
  } 
  return 
$str_out

PradeepKr is offline   Reply With Quote
Reply

Tags
php , replace , token

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
Pingbacks are On
Refbacks are On



» Sponsors

» Links

» Affiliates
Web Hosting
Online Backup Reviews
Marketing Find
Merchant Select
SiteMap Builder
Host Compare

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 05:01 PM.


Powered by vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2
Webmaster Forums
Web Hosting | Chicago Web Hosting | Web Hosting