Prepared statement puts \ before ' - php

Im using prepared statements to descramble BBcode but for some reason it puts \ before ' when posting. I dont know what causes it, but Im sure it happens when i change the BBcode to html to be put in the database, the code looks like this:
$text = $membership->remove_HTML($text);
//convert line breaks to <br /> tags.
$text = nl2br($text);
//cleans up by removing white space.
$text = trim($text);
//now lets replace things BASIC EDITOR
$text = preg_replace("/\[b\](.*)\[\/b\]/", "<strong>\\1</strong>", $text);
$text = preg_replace("/\[i\](.*)\[\/i\]/", "<em>\\1</em>", $text);
$text = preg_replace("/\[u\](.*)\[\/u\]/", "<span style='text-decoration:underline;'>\\1</span>", $text);
$text = preg_replace("/\[s\](.*)\[\/s\]/", "<del>\\1</del>", $text);
$text = preg_replace("/\[url\](.*)\[\/url\]/", "<a target='_blank' href='\\1'>\\1</a>", $text);
$text = preg_replace("/\[url=(.*)\](.*)\[\/url\]/", "<a target='_blank' rel='\\1' href='\\1'>\\2</a>", $text);
//now lets replace MORE things EXPANDED EDITOR
$text = preg_replace("/\[img\](.*)\[\/img\]/", "<img>\\1</img>", $text);
$text = str_ireplace("[hr]","<hr>", $text);
$text = preg_replace("/\[justify\](.*)\[\/justify\]/", "<p style='text-align:justify;'>\\1</p>", $text);
$text = preg_replace("/\[center\](.*)\[\/center\]/", "<p style='text-align:center;'>\\1</p>", $text);
$text = preg_replace("/\[left\](.*)\[\/left\]/", "<p style='text-align:left;'>\\1</p>", $text);
$text = preg_replace("/\[right\](.*)\[\/right\]/", "<p style='text-align:right;'>\\1</p>", $text);
$text = preg_replace("/\[h1\](.*)\[\/h1\]/", "<h4>\\1</h4>", $text);
$text = preg_replace("/\[h2\](.*)\[\/h2\]/", "<h5>\\1</h5>", $text);
$text = preg_replace("/\[h3\](.*)\[\/h3\]/", "<h6>\\1</h6>", $text);
$updatenews = $mysql->add_news($_SESSION['user'][0], $headline, $text, $time);

The quickest thing to check is making sure PHP's Magic Quotes are disabled.
If you don't want to get right into mucking around with your PHP configuration, then check to see if the slashes are present before you roll $text through that series of preg_replaces().

Related

How can I fix CURL quotes strpos() problem?

Firstly, sorry my bad English.
I'm taking text from a site with Curl. After that, I am searching in text some words with strpos().
But; when text coming with quotes, my function is not working. For example. Text is coming with curl without quotes;
$text = "This is my text.";
$intext = "This is";
if (strpos($text, $intext) !== false) {
echo "OK";
}
Okey, page give me "OK", now my codes working.
But, when text coming with quotes like this:
$text = "This's my text.";
$intext = "This's my";
if (strpos($text, $intext) !== false) {
echo "OK";
} else {
echo "NO";
}
The page gives me: "NO"!
Why? I think the quotation mark data from the website is different. How can I fix this problem? I need to compare without clearing punctuation.
I fixed problem with this code;
$str = str_replace(' ', ' ', $text);
$str = html_entity_decode($str, ENT_QUOTES | ENT_COMPAT , 'UTF-8');
$str = html_entity_decode($str, ENT_HTML5, 'UTF-8');
$str = html_entity_decode($str);
$str = htmlspecialchars_decode($str);
$text = strip_tags($str);
Thanks.

Convert \n in a clear space

i have a problem with a function in php i want to convert all the "\n" in a clear space, i've tried with this, but it doesn't work
function clean($text) {
if ($text === null) return null;
if (strstr($text, "\xa7") || strstr($text, "&")) {
$text = preg_replace("/(?i)(\x{00a7}|&)[0-9A-FK-OR]/u", "", $text);
}
$text = htmlspecialchars($text, ENT_QUOTES, "UTF-8");
if (strstr($text, "\n")) {
$text = preg_replace("\n", "", $text);
}
return $text;
}
This is wat i want remove
The site: click here
If you literally have "\n" in your text, which appears to be the case from your screenshots, then do the following:
$text = str_replace("\\n", '', $text);
\n is a special character in PHP that creates new lines, so we need to add the escape character \ in front of it in order to remove text instances of "\n".
preg_replace() seems to work better this way:
$text = preg_replace('/\n/',"",$text);
Single quotes enforce no substitution when sending your pattern to the parser.

PHP preg_replace multiple url replaces

Hey I am trying to do 2 preg_replace:
1.make all urls to html links
2.make all images url to html img tag
But these regexs cancel the other one
<?php
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$reg_exImg = "!http://[a-z0-9\-\.\/]+\.(?:jpe?g|png|gif)!Ui";
// The Text you want to filter for urls
$text = "The text you want to filter goes here. http://google.comhttp://www.ynet.co.il http://dogsm.files.wordpress.com/2011/12/d7a1d7a7d795d791d799-d793d795.jpg";
// Check if there is a url in the text
$text = preg_replace($reg_exImg, '<img src=$0 >', $text);
$text = preg_replace($reg_exUrl, '$0', $text);
echo $text;
?>
How can I make that the preg_replace that make url to links dont do this to the tag?
Thanks.
Haim
This might be better as a callback.
Use just the $reg_exUrl, and do this:
$text = preg_replace_callback($reg_exUrl,function($m) {
if( preg_match("/\.(?:jpe?g|png|gif)$/i",$m[0])) {
return "<img src='".$m[0]."' />";
}
else {
return "<a href='".$m[0]."' rel='nofollow'>".$m[0]."</a>";
}
},$text);

Find a Link in PHP String and convert it into a Hyperlink?

Find a Link in PHP String and convert it into a Hyperlink so it becomes Clickable and opens in New Tab.
PHP Code / String:
<?php echo $post_details['description']; ?>
Test Link:
http://www.test.com
use this library. it automatically detect link from the string and make it clickable. you don't need to create a link just add a string having a link. this library will detect it automatically.
http://soapbox.github.io/jQuery-linkify/
Use this example to solve your problem
<?php
function makeClickableLink($text) {
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9#:%_\+.~#?&//=]+)', '\\1', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9#:%_\+.~#?&//=]+)', '\\1\\2', $text);
$text = eregi_replace('([_\.0-9a-z-]+#([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '\\1', $text);
return $text;
}
// Usage
// Email address example
$text = "you#example.com";
echo makeClickableLink($text);
// URL example
$text = "http://www.example.com";
echo makeClickableLink($text);
// FTP URL example
$text = "ftp://ftp.example.com";
echo makeClickableLink($text);
?>

Php link active

Well this is the function which active the link if someone put a link in message box with text.
My question is it doesn't show more than 1 link if someone put many link ex: www.yahoo.com www.gmail.com www.facebook.com, Then it's show only first link www.yahoo.com
function txt2link($text){
// force http: on www.
$text = ereg_replace( "www\.", "http://www.", $text );
// eliminate duplicates after force
$text = ereg_replace( "http://http://www\.", "http://www.", $text );
$text = ereg_replace( "https://http://www\.", "https://www.", $text );
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
$text = preg_replace($reg_exUrl, ''.$url[0].'', $text);
} // if no urls in the text just return the text
return ($text);
}
$url = "Alter pot waer it your pot http://css-tricks.com/snippets/php/find-urls-in-
text-make-links/ you may click the link www.yahoo.com or you may see what is the
http://www.youtube.com say, is it right?";
echo txt2link($url);
you can run this code to see the result.
Any Idea ?
This is one that someone here helped me build up a while back, I dunno where it is on stack anymore but I still use this function to date.. This handles many urls in one shot, with or without http: with or without www. in most cases, its true this could us a bit of refining, but in all does the job really nice.
//for finding URLs within body of text and converting them to a clickable link
//checks DNS to see if its valid and if the link HTML already exists ignores it.
function titleHyper($text){
$text = preg_replace( "/(www\.)/is", "http://", $text);
$text = str_replace(array("http://http://","http://https://"), "http://", $text);
$text = str_replace(array("<a href='", "<a href=\"", "</a>", "'>", "\">"), "", $text);
$reg_exUrl = "/(http|https|ftp|ftps|)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
preg_match_all($reg_exUrl, $text, $matches);
$usedPatterns = array();
$context = stream_context_create(array(
'http' => array(
'timeout' => 5
)
));
foreach($matches[0] as $pattern){
if(!array_key_exists($pattern, $usedPatterns)){
$usedPatterns[$pattern]=true;
$the_contents = #file_get_contents($pattern, 0, $context);
if(substr(trim($pattern), 0, 8) != "https://"){
$color = "#FF0000";
}
if (empty($the_contents)) {
$title = $pattern;
} else {
preg_match("/<title>(.*)<\/title>/Umis", $the_contents, $title);
$title = $title[1];
$color = "#00FF00";
//$title = htmlspecialchars($title, ENT_QUOTES); //saving data to database
}
$text = str_ireplace($pattern, "<a style='font-size: 14px; background-color: #FFFFFF; color: $color;' href='$pattern' rel='nofollow' TARGET='_blank'> $title </a>", $text);
}
}
return $text;
}
// titleHyper() in action example:
//$text = "Some sample text with WWW.AOL.com<br />http://www.youtube.com/watch?v=YaxKiZfQcX8 <br />Anyone use www.myspace.com? <br />Some people are nuts, look at this stargate link at http://www.youtube.com/watch?v=ZKoUm6z5SzU&feature=grec_index , like aliens exist or something. http://www.youtube.com/watch?v=sfN-7HczmOU&feature=grec_index and here's a secure site https://familyhistory.hhs.gov, unless you use curl or allow secure connections it will never get a title. <br /> This is a not valid site http://zzzzzzz and this is a dead site http://zwzwzwxzw.com.<br /> Lastly lets try an already made hyperlink and see what it does <a href='http://tacobell.com'>taco bell</a>";
//echo titleHyper($text);

Categories