We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,007 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How can you replace html only when needed?

I am makeing a comment system, i have a string where i remove html code:

$area = stripslashes(htmlspecialchars($area));

But i replace text sometimes by code, but it doesn't work because of the code above:

$txtSmileys = array (':-)', ':-(');
$imgSmileys = array ('<img src="smiley.jpg" />', '<img src="smiley-sad.jpg" />');
$text = str_replace($txtSmileys, $imgSmileys, $text);

So how can i remove html before replacing?

Sanchixx

4
Contributors
7
Replies
9 Hours
Discussion Span
8 Months Ago
Last Updated
8
Views
Sanchixx
Junior Poster in Training
80 posts since May 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

Reverse the order, replace the smileys first, then use the strip and special functions.

pritaeas
Posting Prodigy
Moderator
9,287 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,458
Skill Endorsements: 86

That doesn't work i've already tried it. :(

Sanchixx
Junior Poster in Training
80 posts since May 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

Have you ever tried to use JQUERY? :) could be helpful!

esma.ramirez
Newbie Poster
7 posts since Aug 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

How?

Sanchixx
Junior Poster in Training
80 posts since May 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

I solved it! I used htmlspecialchars_decode($Astring);

Sanchixx
Junior Poster in Training
80 posts since May 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0
<?php
    $input = "<p>This is an example of random input you might encounter! :)</p>";

    $output = strip_tags($input);

    $output = str_replace (":)", "<img src=\"https://www.google.ca/intl/en_ALL/images/logos/images_logo_lg.gif\" alt=\"\"/>", $output);

    echo $output;
?>

Remember that you need the slashes infront of the quotes so that PHP ignores the quotes (because they're not really the end quotes).

JameB
Junior Poster
166 posts since Mar 2009
Reputation Points: 76
Solved Threads: 15
Skill Endorsements: 1
Sanchixx
Junior Poster in Training
80 posts since May 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0786 seconds using 2.71MB