PHP Text Replace Problem

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
bibsta
Forum Newbie
Posts: 3
Joined: Mon May 14, 2007 8:03 pm

PHP Text Replace Problem

Post by bibsta »

Hi, This is my 1st php project and its been a case of learn as i go ask my friends if i get stuck but seems like all my friends have hit a problem to im trying to replace at the moment smiley codes into word but without any luck i didnt manage to get this far all the everything works perfect but the dam text replace dont work please help its driving me nuts.Thanks in advance code below:

Functions Page:

Code: Select all

<?php
function strip_tags_except($text, $allowed_tags, $strip=TRUE) {
  if (!is_array($allowed_tags))
    return $text;

  if (!count($allowed_tags))
    return $text;

  $open = $strip ? '' : '<';
  $close = $strip ? '' : '>';

  preg_match_all('!<\s*(/)?\s*([a-zA-Z]+)[^>]*>!',
    $text, $all_tags);
  array_shift($all_tags);
  $slashes = $all_tags[0];
  $all_tags = $all_tags[1];
  foreach ($all_tags as $i => $tag) {
    if (in_array($tag, $allowed_tags))
      continue;
    $text =
      preg_replace('!<(\s*' . $slashes[$i] . '\s*' .
        $tag . '[^>]*)>!', $open . '$1' . $close,
        $text);
  }

  return $text;
}

function url_to_link($text) {
  $text =
    preg_replace('!(^|([^\'"]\s*))' .
      '([hf][tps]{2,4}:\/\/[^\s<>"\'()]{4,})!mi',
      '$2<a href="$3">$3</a>', $text);
  $text =
    preg_replace('!<a href="([^"]+)[\.:,\]]">!',
    '<a href="$1">', $text);
  $text = preg_replace('!([\.:,\]])</a>!', '</a>$1',
    $text);
  return $text;
}
THIS IS WHERE THE TROUBLE STARTS  
 function smiley($text) {
	$orignal  = array(":)", ":(");
	$replace = array("smile", "sad");
  $text1 = str_replace($orignal,$replace, $text); 
  return $text1; 
  }
?>
Add Page:

Code: Select all

<?php
include("includes/connection.php");
include("includes/functions.php");

$content  = $_POST['content'];

$content=url_to_link($content);
$content=smiley($content);

$title=$db->real_escape_string($_POST['title']);
$content=$db->real_escape_string($_POST['content']);
$author=$db->real_escape_string($_POST['author']);
$date=date("y-m-d"); 
$time=date("H:i:s");
$image=$db->real_escape_string($_POST['image']);
$cat=$db->real_escape_string($_POST['cat']);

$title=strip_tags_except($title, array('a'), FALSE);
$content=strip_tags_except($content, array('a','b','u'), FALSE);
$author=strip_tags_except($author, array('a'), FALSE);
$date=strip_tags_except($date, array('a'), FALSE);
$time=strip_tags_except($time, array('a'), FALSE);
$image=strip_tags_except($image, array('a'), FALSE);
$cat=strip_tags_except($cat, array('a'), FALSE);

// Query to use.
$query = "INSERT INTO `manage`.`review` (`id` ,`title` ,`content` ,`image` ,`author` ,`date` ,`time` ,`cat`) VALUES (NULL , '$title', '$content', '$image', '$author', '$date', '$time', '$cat');";

// All queries and commands go here.
if (!$query = $db->query($query)) {
	die('Error with query.');
}

// Close $db connection
$db->close();

header( 'Location: cat_index.php' ) ;

?>
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

first you the function to replace the faces and it does what is supposed to do, but few lines below that you set the variable again from the POST

Code: Select all

$content  = $_POST['content']; 

$content=url_to_link($content); 
$content=smiley($content); 

$title=$db->real_escape_string($_POST['title']); 
$content=$db->real_escape_string($_POST['content']);
try this

Code: Select all

$content  = $_POST['content']; 

$content=url_to_link($content); 
$content=smiley($content); 

$title=$db->real_escape_string($_POST['title']); 
$content=$db->real_escape_string($content);
that should work now.
bibsta
Forum Newbie
Posts: 3
Joined: Mon May 14, 2007 8:03 pm

Post by bibsta »

I really appreciate the time you have taken to solve my problem I can’t thank you enough many thanks :D
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

it only took me 30 sec. but you are welcome.
Post Reply