Page 1 of 1

PHP Text Replace Problem

Posted: Mon May 14, 2007 8:14 pm
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' ) ;

?>

Posted: Tue May 15, 2007 9:45 am
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.

Posted: Tue May 15, 2007 6:30 pm
by bibsta
I really appreciate the time you have taken to solve my problem I can’t thank you enough many thanks :D

Posted: Wed May 16, 2007 1:48 am
by louie35
it only took me 30 sec. but you are welcome.