PHP Text Replace Problem
Posted: Mon May 14, 2007 8:14 pm
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:
Add Page:
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;
}
?>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' ) ;
?>