preg_replace to replace string for matching url - php

function makeLinksInTheContent($html)
{
$html= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" rel=\"nofollow\" >$3</a>", $html);
$html= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" rel=\"nofollow\" >$3</a>", $html);
$html= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)#([\w\-]+\.([\w\-\.]+)+)/i", "$1$2#$3", $html);
return($html);
}
this is my code.
My need is autolinking the url. Using preg_replace to find the url and set link to this url.
for example: "A page contains www.google.com." if i pass this content to makeLinksInTheContent($html), it will return "A page contains www.google.com."
But The following url format is not getting linked.
(http://www.google.com)
www.test.com()and[]&^%$##!+|!##$%^&()_+}{:"?><,./;'[]=-09~`.co,in,com.com
http://www.test.com()and[]&^%$##!+|!##$%^&()_+}{:"?><,./;'[]=-09~`.co,in,com.com
https://www.test.com()and[]&^%$##!+|!##$%^&()_+}{:"?><,./;'[]=-09~`.co,in,com.com
ftp://www.test.com()and[]&^%$##!+|!##$%^&()_+}{:"?><,./;'[]=-09~`.co,in,com.com
I think my regular expression have some mistakes. please suggest us.

You can use preg_replace_callback in this case. Read more
Function
<?php
function replace_urls( $text = null ) {
$regex = '/((http|ftp|https):\/\/)?[\w-]+(\.[\w-]+)+([\w.,#?^=%&:\/~+#-]*[\w#?^=%&\/~+#-])?/';
return preg_replace_callback( $regex, function( $m ) {
$link = $name = $m[0];
if ( empty( $m[1] ) ) {
$link = "http://".$link;
}
return ''.$name.'';
}, $text );
}
?>
Usage
<?php
$text = "http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054
www.google.com
https://twitter.com/
http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar";
echo replace_urls( $text );
?>
Output
http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054
www.google.com
https://twitter.com/
http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar

My answer to the last time you asked this:
preg_replace to replace string for matching url
I can suggest using my function - just tested with your data and works for me
function parse_links($string,$mailto=true)
{
$preg_r = "#(((https?|ftp)(://)?)?[a-zA-Z0-9-_.]{1,64}\.[a-zA-Z0-9-_.]{1,128}\.[a-z]{2,4}(\.[a-z]{2,4})?(/([^ ]{1,256}))?/?)#";
$string = preg_replace($preg_r,"$1",explode(" ",$string));
if($mailto)
{
$preg_r2 = "/([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*#([0-9a-zA-Z_\-\.][-\w]*[0-9a-zA-Z_\-\.]\.)+[a-zA-Z]{2,9})/";
$string = preg_replace($preg_r2,"$1",$string);
}
return implode(" ",$string);
}
Just pass false as second parameter if you don't want email links to be created

Related

How to remove "<>" brackets from string in php?

$cont=htmlspecialchars(file_get_contents("https://myanimelist.net/anime/30276/One_Punch_Man"));
function getBetween($string, $start = "", $end = ""){
if (strpos($string, $start)) { // required if $start not exist in $string
$startCharCount = strpos($string, $start) + strlen($start);
$firstSubStr = substr($string, $startCharCount, strlen($string));
$endCharCount = strpos($firstSubStr, $end);
if ($endCharCount == 0) {
$endCharCount = strlen($firstSubStr);
}
return substr($firstSubStr, 0, $endCharCount);
} else {
return '';
}
}
$name=getBetween($cont,'title',' - MyAnimeList.net');
//$name=preg_replace('/[^a-zA-Z0-9 \p{L}]/m', '', $name);
preg_replace('/(*UTF8)[\>\<]/m', '', $name);
trim($name," ");
//$name=str_replace("gt", "", $name);
echo $name;
i want to find the text between title tags. how to do this?
for example in this page title contains 'One Punch Man - MyAnimeList.net' i want to get that
Just use string replace function:
$string = '<BoomBox>';
$string = str_replace('<', '', $string);
$string = str_replace('>', '', $string);
echo $string; // output: Boombox
http://php.net/manual/en/function.str-replace.php
You edited your answer, and we can now see you are dealing with XML/HTML. It's always better to work with the DOM classes. Never use regex! There is a famous Stack Overflow post explaining why never to parse html with regex. Try this solution instead:
<?php
$dom = new DOMDocument();
$dom->loadHTML('<title>BoomBox</title>');
echo $dom->getElementsByTagName('title')->item(0)->textContent;
http://php.net/manual/en/class.domdocument.php
http://php.net/manual/en/class.domnode.php
See it working here https://3v4l.org/EjPQd
You can use preg_replace();, or strip_tags();.
Example preg_replace();:
$str = '> One Punch Man';
$new = preg_replace('/[^a-zA-Z0-9 \p{L}]/m', '', $str);
echo $new;
Output: One Punch Man
Above example will only allow a-z, A-Z and 0-9. You can expand this.
Example strip_tags();:
$str = '<title> BoomBox </title>';
$another = strip_tags($str);
echo $another;
Output: BoomBox
Documentation:
http://php.net/manual/en/function.preg-replace.php // preg_replace();
http://php.net/manual/en/function.strip-tags.php // strip_tags();
You can also use a single call to str_replace with the ['<','>'] as the search argument:
$string = '<BoomBox>';
echo str_replace(['<', '>'], '', $string) . PHP_EOL;
// => Boombox
Or, you may use a regex with preg_replace (especially, if you plan on adding more restrictions for in-context matching to it):
echo preg_replace('~[<>]~', '', $string);
// => Boombox
See the PHP demo.

Create and simplify links from body of text in PHP

I need to extract all links in a body of text in php and make them clickable. The problem is I can't seem to simplify the text of the link in any way.
I tried using preg_replace_callback but I can't seem to get the trimming function working properly:
function trimUrl($url){
$maxLength = 3;
if(strlen($url)>$maxLength){
$urlShort = substr($str,0,$maxLength).'...';
}
else{
$urlShort = $url;
}
return $urlShort;
}
function enableLinks($text){
return preg_replace_callback("!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9#:%_+.~#?&;//=]+)!i", "<a href='$1' target='_blank'>".trimUrl("$1")."</a>", $text);
}
enableLinks("Visit more work at http://www.google.com");
How can I run a second function within the preg_replace_callback that trims the output text?
What if you used a function inside that function. So if the first function evaluates to true then run this next function? And also try using preg_replace_callback in a variable format so its easier to work with
First, you are using substring(). Where have you defined the variable $str? And, if you do this:
$var = preg_replace_callback("!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9#:%_+.~#?&;//=]+)!i", "<a href='$1' target='_blank'>".trimUrl("$1")."</a>", $text);
Than can you use a new function:
return function($var);
Ended up using a more expanded function to achieve this, works on multiple urls with or without "http://":
function trimUrlOutput($url){
$maxLength = 30;
if(strlen($url)>$maxLength){
$urlShort = substr($url,0,$maxLength).'...';
}
else{
$urlShort = $url;
}
return $urlShort;
}
function enableLinks($text){
$text = ereg_replace( "www\.", "http://www.", $text );
$text = ereg_replace( "http://http://www\.", "http://www.", $text );
$text = ereg_replace( "https://http://www\.", "https://www.", $text );
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
if(preg_match_all($reg_exUrl, $text, $url)) {
$matches = array_unique($url[0]);
foreach($matches as $match) {
$linkText = trimUrlOutput($match);
$replacement = "<a href=".$match." target='_blank'>{$linkText}</a>";
$text = str_replace($match,$replacement,$text);
}
return $text;
}
else{
return $text;
}
}
enableLinks("Visit more work at http://www.google.com");
Hope this helps someone.

Delete text after "more" tag (PHP)

How to delete text after !--more-- tag?
I tried:
function addMore($text) {
$text = preg_replace('/!--more--(.*)/', ' ', $text);
return $text;
}
Thanks in advance.
Just delete the string, replacing it with the !--more--:
$text = preg_replace('/!--more--.*/', '!--more--', $text);
$text = "plain to you howhings of the great explorer of the truth.!--more-- plain to you how allt the truth,/";
echo $short = substr($text, 0, strpos( $text, '!--more--'));
Output:
http://codepad.org/atXzthBc
Add the 's' modifier to make the preg work even on break lines:
function addMore($text) {
$text = preg_replace('/!--more--(.*)/s', ' ', $text);
return $text;
}
look here:
http://php.net/manual/en/reference.pcre.pattern.modifiers.php
YOu can use explode function or simply substr as below :
$text = substr($text, 0, strpos($text, '!-more-'));
function addMore($text) {
$newtext=explode("!--more--",$text);
return $newtext[0];
}

Use PHP to Convert Email Text to a Link

Based on a question I had answered here ( Use PHP to Replace HTML with HTML ), I'd like to be able to filter my output text for email addresses, and convert those text emails to "mailto" links.
Here's the PHP code that works, but only for converting some HTML to other HTML. What I've tried to do is have this function look for an email address, and convert it to a "mailto" link. For whatever reason, the code doesn't convert the email addresses. Here's my PHP:
function text_filter($string) {
$search = array('<p>__</p>', '/[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/');
$replace = array('<hr />', '$2');
$processed_string = str_replace($search, $replace, $string);
echo $processed_string;
}
When I use this function for output, this is what the code looks like:
<?php text_filter( get_the_content() ); ?>
str_replace() doesn't use regular expressions, rewritten with preg_replace().
Added delimiters to first matching expression.
Fixed replacement from $1 to $2.
function text_filter($string) {
$search = array('/<p>__<\/p>/', '/([a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/');
$replace = array('<hr />', '$1');
$processed_string = preg_replace($search, $replace, $string);
echo $processed_string;
}
#Adam Baney - This will work even when used repeatedly.
// EMAILS
$str = preg_replace('~(^|[\s\.,;\n\(])([a-zA-Z0-9._+-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})~',
'$1$2',
$str);
// PHONE NUMBERS
$str = preg_replace_callback('~(^|[\s\.,;\n\(])(?<! )([0-9 \+\(\)]{9,})~', function($m) {
return $m[1].''.$m[2].'';
}, $str);
You can't use str_replace to do a regular expression replace.
You will need to split the actions up.
function text_filter($string) {
$search = array('<p>__</p>');
$replace = array('<hr />');
$processed_string = str_replace($search, $replace, $string);
$processed_string = preg_replace('/[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/','$2',$processed_string);
echo $processed_string;
}
See: http://www.php.net/manual/en/function.preg-replace.php for preg replacing.
function obfuscate_email($content){
$pattern = '#([0-9a-z]([-_.]?[0-9a-z])*#[0-9a-z]([-.]?[0-9a-z])*\\.';
$pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
$replacement = '\\1';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
And add filter
add_filter( 'the_content', 'obfuscate_email' );
Another way to do it in order so that it would work with existing html links in text:
function html_parse_text($text)
{
$text = preg_replace("/(?<!\")(((f|ht){1}tps?:\/\/)[-a-zA-Z0-9#:%_\+.~#?&\/\/=]+)/",
'<a href="\\1" target=_blank>\\1</a>', $text);
$text = preg_replace("/([[:space:]()[{}])(www.[-a-zA-Z0-9#:%_\+.~#?&\/\/=]+)/",
'\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
$text = preg_replace("/(?<!\")([_\.0-9a-z-]+#([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/",
'<a href="mailto:\\1" target=_blank>\\1</a>', $text);
return $text;
}
Here is another version of this that seems to work for me. I've added + char to handle "plus addressing" (like some+email#address.com)
function replaceemail($text) {-
$ex = "/([a-zA-Z0-9._+-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/";
preg_match_all($ex, $text, $url);
foreach($url[0] as $k=>$v) $text = str_replace($url[0][$k], ''.$url[0][$k].'', $text);
return $text;
}

Swap all youtube urls to embed via preg_replace()

Hello I'm trying to convert youtube links into embed code.
this is what I have:
<?php
$text = $post->text;
$search = '#<a(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*$#x';
$replace = '<center><iframe width="560" height="315" src="http://www.youtube.com/embed/$2" frameborder="0" allowfullscreen></iframe></center>';
$text = preg_replace($search, $replace, $text);
echo $text;
?>
It works for one link. However if I add two, it will only swap the last occurrence. What do I have to change?
You're not handling the end of the string properly. Remove the $, and replace it with the closing tag </a>. this will fix it.
$search = '#<a(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*<\/a>#x';
$replace = '<center><iframe width="560" height="315" src="http://www.youtube.com/embed/$2" frameborder="0" allowfullscreen></iframe></center>';
$text = preg_replace($search, $replace, $text);
Try this : preg_replace($search, $replace, $text, -1);
I know it's the default but who knows...
EDIT Try that if not working ;
do{
$text = preg_replace($search, $replace, $text, -1, $Count);
}
while($Count);
Here are a regular expresion more efficient: http://pregcopy.com/exp/26, tralate this to PHP: (add "s" modifier)
<?php
$text = $post->text;
$search = '#<a (?:.*?)href=["\\\']http[s]?:\/\/(?:[^\.]+\.)*youtube\.com\/(?:v\/|watch\?(?:.*?\&)?v=|embed\/)([\w\-\_]+)["\\\']#ixs';
$replace = '<center><iframe width="560" height="315" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe></center>';
$text = preg_replace($search, $replace, $text);
echo $text;
?>
Test it
There are two types of youtube link for one video:
Example:
$link1 = 'https://www.youtube.com/watch?v=NVcpJZJ60Ao';
$link2 = 'https://www.youtu.be/NVcpJZJ60Ao';
This function handles both:
function getYoutubeEmbedUrl($url)
{
$shortUrlRegex = '/youtu.be\/([a-zA-Z0-9_-]+)\??/i';
$longUrlRegex = '/youtube.com\/((?:embed)|(?:watch))((?:\?v\=)|(?:\/))([a-zA-Z0-9_-]+)/i';
if (preg_match($longUrlRegex, $url, $matches)) {
$youtube_id = $matches[count($matches) - 1];
}
if (preg_match($shortUrlRegex, $url, $matches)) {
$youtube_id = $matches[count($matches) - 1];
}
return 'https://www.youtube.com/embed/' . $youtube_id ;
}
The output of $link1 or $link2 would be the same :
$output1 = getYoutubeEmbedUrl($link1);
$output2 = getYoutubeEmbedUrl($link2);
// output for both: https://www.youtube.com/embed/NVcpJZJ60Ao
Now you can use the output in iframe!

Categories