Using two functions on one echo variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
likeskoolaid
Forum Newbie
Posts: 1
Joined: Sat Sep 03, 2011 5:43 pm

Using two functions on one echo variable

Post by likeskoolaid »

I have two functions, one turns un-href links into working href links.

Code: Select all

<?php
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
?>

The other replaces an array of words into a different word.

Code: Select all

<?php
$replace = array(
'Cat' => 'Dog',
);
?>

This is how you echo both separately, the problem I'm having and need help with is combining both functions into one echo so both fixes apply to a single variable ($content).

Code: Select all

<?php echo str_replace_assoc($replace,$content); ?>

<?php echo preg_replace(a$reg_exUrl, '<a href="'.$url[0].'" target="_blank" class="outboundlink" rel="nofollow">'.$url[0].'</a>', $content); ?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Using two functions on one echo variable

Post by Christopher »

You can pass arrays of regex replacements to preg_replace (you can also pass arrays to str_replace). Or simpler would be just:

Code: Select all

$content = str_replace_assoc($replace,$content);
$content = echo preg_replace($reg_exUrl, '<a href="'.$url[0].'" target="_blank" class="outboundlink" rel="nofollow">'.$url[0].'</a>', $content);
echo $content;
(#10850)
Post Reply