i have this PHP chatbox.
If i would type a link in the chatbox, it would not display it as a link.
How can i use STR replace to do this?
It should respond to stuff like 'http' 'http://' '.com' '.nl' 'www' 'www.' ....
My other STR replace lines look like these:
$bericht = str_replace ("STRING1","STRINGREPLACEMENT1",$bericht);
Someone?
Hey! Try this code (found at php.net somewhere):
function format_urldetect( $text )
{
$tag = " rel=\"nofollow\"";
//
// First, look for strings beginning with http:// that AREN't preceded by an <a href tag
//
$text = preg_replace( "/(?<!\\0", $text );
//
// Second, look for strings with casual urls (www.something.com...) and make sure they don't have a href tag OR a http:// in front,
// since that would have been caught in the previous step.
//
$text = preg_replace( "/(?<!\\0", $te
xt );
$text = preg_replace( "/(?<!\\0", $t
ext );
return $text;
}
Uhm, broken indentation. try this http://triop.se/code.txt
You should use regex instead. look at preg_replace
$regex = '`(\s|\A)(http|https|ftp|ftps)://(.*?)(\s|\n|[,.?!](\s|\n)|$)`ism';
$replace = '$1$2://$3$4'
$buffer = preg_replace($regex,$replace,$buffer);
Related
So I currently have this...
<?php
$textblockwithformatedlinkstoecho = preg_replace('!(((f|ht)tp(s)?://)[-a-zA-
Zа-яА-Я()0-9#:%_+.~#?&;//=]+)!i', '$1',
$origtextwithlinks);
echo $textblockwithformatedlinkstoecho;
?>
But, I would like to also shorten the clickable link to around 15 chars in length...
Example input text
I recommend you visit http://www.example.com/folder1/folder2/page3.html?
longtext=ugsdfhsglshghsdghlsg8ysd87t8sdts8dtsdtygs9ysd908yfsd0fyu for more
information.
Required output text
I recommend you visit example.com/fol... for more information.
You can use preg_replace_callback() to manipulate the matches.
Example:
$text = "I recommend you visit http://www.example.com/folder1/folder2/page3.html?longtext=ugsdfhsglshghsdghlsg8ys\d87t8sdts8\dtsdtygs9ysd908yfsd0fyu for more information.";
$fixed = preg_replace_callback(
'!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9#:%_+.~#?&;//=]+)!i',
function($matches) {
// Get the fully matched url
$url = $matches[0];
// Do some magic for the link text, like only show the first 15 characters
$text = strlen($url) > 15
? substr($url, 0, 15) . '...'
: $url;
// Return the new html link
return '' . $text . '';
},
$text
);
echo $fixed;
You probably need to modify your regex though, since it doesn't match the \-characters you have in the query string in the url.
I have a string for example : I am a boy
I want to show this on my url for example in this way : index.php?string=I-am-a-boy
My program :
$title = "I am a boy";
$number_wrds = str_word_count($title);
if($number_wrds > 1){
$url = str_replace(' ','-',$title);
}else{
$url = $title;
}
What if I have a string : Destination - Silicon Valley
If I implement the same logic my url will be : index.php?string=Destination---Silicon-Valley
But I want to show only 1 hyphen.
I want to show a hyphen instead of a plus sign..
url_encode() will eventually insert plus symbols.. So it's not helping here.
Now if I use minus symbol then if the actual string is Destination - Silicon Valley, then the url will look like
Destination-Silicon-Valley and not
Destination---Silicon-Valley
Check this stackoverflow question title and the url. You will know what I am saying.
Check this
Use urlencode() to send strings along with an url:
$url = 'http://your.server.com/?string=' . urlencode($string);
In comments you told, that you don't want urlencode, you'll just replace spaces by - characters.
First, you should "just do it", the if conditional and str_word_count() is just overhead. Basically your example should look like this:
$title = "I am a boy";
$url = str_replace(' ','-', $title);
That's it.
Further you told that this would make problems if the original string already contains a -. I would use preg_replace() instead of str_replace() to solve that problem. Like this:
$string = 'Destination - Silicon Valley';
// replace spaces by hyphen and
// group multiple hyphens into a single one
$string = preg_replace('/[ -]+/', '-', $string);
echo $string; // Destination-Silicon-Valley
Use preg_replace instead:
$url = preg_replace('/\s+/', '-', $title);
\s+ means "any whitespace character (\t\r\n\f (space, tab, line feed, newline)).
use urlencode:
<?php
$s = "i am a boy";
echo urlencode($s);
$s = "Destination - Silicon Valley";
echo urlencode($s);
?>
return:
i+am+a+boy
Destination+-+Silicon+Valley
and urldecode:
<?php
$s = "i+am+a+boy";
echo urldecode($s)."\n";
$s = "Destination+-+Silicon Valley";
echo urldecode($s);
?>
return:
i am a boy
Destination - Silicon Valley
just use urlencode() and urldecode(). It’s for sending Data with GET in the URL.
I have this replace regex (it's taken from the phpbb source code).
$match = array(
'#<!\-\- ([mw]) \-\-><a (?:class="[\w-]+" )?href="(.*?)" target\=\"_blank\">.*?</a><!\-\- \1 \-\->#',
'#<!\-\- .*? \-\->#s',
'#<.*?>#s',
);
$replace = array( '\2', '', '');
$message = preg_replace($match, $replace, $message);
If I run it through a message like this
asdfafdsfdfdsfds
<!-- m --><a class="postlink" href="http://website.com/link-is-looooooong.txt">http://website.com/link ... oooong.txt</a><!-- m -->
asdfafdsfdfdsfds4324
It returns this
asdfafdsfdfdsfds
http://website.com/link ... oooong.txt
asdfafdsfdfdsfds4324
However I would like to make it into a replace function. So I can replace the link title in a block by providing the href.
I want to provide the url, new url and new title. So I can run a regex with these variables.
$url = 'http://website.com/link-is-looooooong.txt';
$new_title = 'hello';
$new_url = 'http://otherwebsite.com/';
And it would return the same raw message but with the link changed.
<!-- m --><a class="postlink" href="http://otherwebsite.com/">hello</a><!-- m -->
I've tried tweaking it into something like this but I can't get it right. I don't know how to build up the matched result so it has the same format after replacing.
$message = preg_replace('#<!\-\- ([mw]) \-\-><a (?:class="[\w-]+" )?href="'.preg_quote($url).'" target\=\"_blank\">(.*?)</a><!\-\- \1 \-\->#', $replace, $message);
You'll find that parsing HTML with regex can be a pain and get very complex. Your best bet is to use a DOM parser, like this one, and modify the links with that instead.
You need to catch the other parts in groups as well and then use them in the replacement. try something like this:
$replace = '\1http://otherwebsite.com/\3hello\4';
$reg = '#(<!-- ([mw]) --><a (?:class="[\w-]+" )?href=")'.preg_quote($url).'("(?: target="_blank")?>).*?(</a><!-- \2 -->)#';
$message = preg_replace($reg, $replace, $message);
See here.
Say i have a string of text such as
$text = "Hello world, be sure to visit http://whatever.com today";
how can i (probably using regex) insert the anchor tags for the link (showing the link itself as the link text) ?
You can use regexp to do this:
$html_links = preg_replace('"\b(https?://\S+)"', '$1', $text);
I write this function.
It replaces all the links in a string. Links can be in the following formats :
www.example.com
http://example.com
https://example.com
example.fr
The second argument is the target for the link ('_blank', '_top'... can be set to false).
Hope it helps...
public static function makeLinks($str, $target='_blank')
{
if ($target)
{
$target = ' target="'.$target.'"';
}
else
{
$target = '';
}
// find and replace link
$str = preg_replace('#((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.~]*(\?\S+)?)?)*)#', '<a href="$1" '.$target.'>$1</a>', $str);
// add "http://" if not set
$str = preg_replace('/<a\s[^>]*href\s*=\s*"((?!https?:\/\/)[^"]*)"[^>]*>/i', '<a href="http://$1" '.$target.'>', $str);
return $str;
}
Edit: Added tilde to make urls work better https://regexr.com/5m16v
I'm using the following code to add <span> tags behind <a> tags.
$html = preg_replace("~<a.*?href=\"$url\".*?>.*?</a>~i", "$0<span>test</span>", $html);
The code is working fine for regular links (ie. http://www.google.com/), but it will not perform a replace when the contents of $url are $link$/3/.
This is example code to show the (mis)behaviour:
<?php
$urls = array();
$urls[] = '$link$/3/';
$urls[] = 'http://www.google.com/';
$html = 'Test Link' . "\n" . 'Google';
foreach($urls as $url) {
$html = preg_replace("~<a.*?href=\"$url\".*?>.*?</a>~i", "$0<span>test</span>", $html);
}
echo $html;
?>
And this is the output it produces:
Test Link
Google<span>test</span>
$url = preg_quote($url, '~'); the dollar signs are interpreted as usual: end-of-input.
just somebody is correct; you must escape your special regex characters if you mean for them to be interpreted as literal.
It also looks to me like it can't perform the replace because it never makes a match.
Try replacing this line:
$urls[] = '$link$/3/';
With this:
$urls[] = '$link/3/';
$ is considered a special regex character and needs to be escaped. Use preg_quote() to escape $url before passing it to preg_replace().
$url = preg_quote($url, '~');
$ has special meaning in regex. End of line. Your expression is being expanded like this:
$html = preg_replace("~<a.*?href=\"$link$/3/\".*?>.*?</a>~i", "$0<span>test</span>", $html);
Which fails because it can't find "link" between two end of lines. Try escaping the $ in the $urls array:
$urls[] = '\$link\$/3/';