For some reason str_replace() does not work with /. I am creating a function to accept a unique linking style in input and text area forms for a blog CMS that I am making. For instance, [{http://brannondorsey.com}My Website] will be translated to <a href='http://brannondorsey.com'>My Website</a> when passed through make_link($string);. Here is my code:
function make_link($input){
$double = str_replace( '"', '"', $input);
$single = str_replace("'", "'", $double);
$bracket_erase = str_replace('[', "", $single);
$link_open = str_replace('{', '<a href="', $bracket_erase);
$link_close = str_replace("}", ">", $link_open);
$link_value = str_replace(']', "</a>", $link_close);
echo $link_value;
}
Everything works correctly except for ] is not replaced with </a>. If I remove the slash it will successfully replace ] with <a>, however, as we all know, that does not properly close an anchor tag and therefor makes all html content between the {and the next closing anchor tag in my webpage a link.
You might want to go down the regular expression route for this.
function make_link($link){
return preg_replace('/\[{(.*?)}(.*?)\]/i', '$2', $link);
}
I personally suggest the preg_replace answer of Marcus Recck below rather than mine here.
its there just not seen because the browser wont show html, but you can use the below to see it, and\or use the browsers view source option
$link_close ="]";
$link_value = str_replace(']', "</a>", $link_close);
echo htmlspecialchars($link_value);//= </a>
var_dump ($link_value); //=string(4) "" [invisible due to browser, but the 4 tells you its there]
the finial version of the OP's function:
function make_link($input){
$double = str_replace( '"', '"', $input);
$single = str_replace("'", "'", $double);
$bracket_erase = str_replace('[', "", $single);
$link_open = str_replace('{', '<a href="', $bracket_erase);
$link_close = str_replace("}", '">', $link_open);
$link_value = str_replace(']', "</a>", $link_close);
return $link_value;
}
echo htmlspecialchars(make_link('[{http://brannondorsey.com}My Website]'));
Related
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.
So, I am using this to replace BBCode to HTML:
$text = htmlspecialchars($text);
$advanced_bbcode = array(
'#\[quote](\r\n)?(.+?)\[/quote]#si',
'#\[url](.+)\[/url]#Usi');
$advanced_html = array(
'<blockquote class="quote">$2</blockquote>',
'<a rel="nofollow" target="_blank" href="$1">$1</a>');
$text = preg_replace($advanced_bbcode, $advanced_html,$text);
echo nl2br($text);
public static function nl2br($var)
{
return str_replace(array('\\r\\n','\r\\n','r\\n','\r\n', '\n', '\r'), '<br />', nl2br($var));
}
This works fine if I only have 1 quote, but If I use multiple quotes like: [quote][quote][quote]first[/quote]second[/quote]end[/quote]
I expect to get:
<blockquote class="quote"><blockquote class="quote"><blockquote class="quote">first</blockquote>second</blockquote>end</blockquote>
But because it takes the first [/qoute] it will turn into:
<blockquote class="quote">[quote][quote]first</blockquote>second[/quote]end[/quote]
I've looked it up but I cant find anything that is working for me. I am new to this kind of stuff.
Thanks.
Make replace until there is BBCode in the string
do {
$text = preg_replace($advanced_bbcode, $advanced_html,$text,-1,$c);
} while($c);
demo
What I want
If the URL in the string contains a .jpg at the end of the URL (not the string) then it should make an image from it with preg_replace else make a normal link.
so for example:
If I have http://www.example.com/images/photo.jpg then it should replace with:
<img src="http://www.example.com/images/photo.jpg" alt="http://www.example.com/images/photo.jpg">
The problem:
The URL is replaced with a link in any way and my regex isn't working :( .
What I have tried:
$content = preg_replace("/(http:\/\/[^\s]+(?=\.jpg))/i","<img src=\"$1\" alt = \"$1\"></img>",$content);
$content = nl2br(preg_replace("/(http:\/\/[^\s]+(?!\.jpg))/m", "$1", $content));
Try this
function replace_links($content)
{
if (preg_match('#(http://[^\s]+(?=\.(jpe?g|png|gif)))#i', $content))
{
$content = preg_replace('#(http://[^\s]+(?=\.(jpe?g|png|gif)))(\.(jpe?g|png|gif))#i', '<img src="$1.$2" alt="$1.$2" />', $content);
}
else
{
$content = preg_replace('#(http://[^\s]+(?!\.(jpe?g|png|gif)))#i', '$1', $content);
}
return $content;
}
$content = preg_replace('#\b(http://\S+\.jpg)\b#i', '<img src="$1" alt="$1" />', $content);
You don't need lookaround. Just go with
$content = preg_replace("#(http://[^ ]+\\.jpg(?= |$)#i","<img src=\"$1\" alt=\"$1\"/>", $content);
I think you used the lookahead operator when you wanted lookbehind. You could change (?=\.jpg) to (?<=\.jpg) but there are other, cleaner regex's I'm sure others will post.
This worked for me.
$parse_img='Hello, http://orbitco-ccna-pastquestions.com/images/Q5.jpg
In the figure above, router R1 has two point-to-point . ';
$parse_img=preg_replace('/(https?:\/\/(.\*)?\\.jpg|png|gif)[\s+]*/i',"< img src=\"$1\" alt = \"$1\">< /img >",$parse_img);
echo $parse_img;
Suyash
i made a function for making a keywords from the post title and replace each word in the full post.
this is my function
function myseonew($title,$text){
$title = stripslashes($title);
$text = stripslashes($text);
$fburl = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$keywords = explode(" ",$title);
$regex = '/('.implode('|', $keywords).')/i';
$output = preg_replace($regex, '<a id="smalltext" href="'.$fburl.'">\\1</a>', $text);
return $output;
}
but i faced a problem for the output the characters all of it comes ( �������� ).
so is there any way to solve this issue
by the way my encoding is UTF-8
regards
try using u modifier in your regexp:
$regex = '/('.implode('|', $keywords).')/iu';
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);