text to link, problem with link that has not https - php

Users can add texts. This texts can have links.
I'd like do add click to it.
The problem is, some links works like:
http://www.example.com
links that has no http will not work and will become:
http://mywebsite.com/www.example.com
any ideas how to solve it?
function toLink($titulo){
$url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';
$titulo = preg_replace($url, '$0', $titulo);
return $titulo;
}

Use preg_replace_callback instead and you can interrogate the match to see if you need to add the protocol.
function toLink($titulo) {
$url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';
$titulo = preg_replace_callback($url, function($matches) {
$url = $matches[0];
if (!preg_match('/^https?:\/\//', $url)) $url = 'http://'.$matches[0];
''.$url.'';
}, $titulo);
return $titulo;
}

Related

PHP preg_match, Finding a package name from Android URL address

I need to get Android package name from the URL address.
Here is what I have done.
$url = 'https://play.google.com/store/apps/details?id=com.gamevil.projectn.global&feature=featured-apps#?t=W251bGwsMSwyLDIwMywiY29tLmdhbWV2aWwucHJvamVjdG4uZ2xvYmFsIl0.';
preg_match("~id=(\d+)~", $url, $matches);
$package_name = $matches[1];
echo $package_name;
Package name should be "com.gamevil.projectn.global"
However, my code is not working.
Is there something that I miss?
you can do this by parse_url function
<?php
$url = 'https://play.google.com/store/apps/details?id=com.gamevil.projectn.global&feature=featured-apps#?t=W251bGwsMSwyLDIwMywiY29tLmdhbWV2aWwucHJvamVjdG4uZ2xvYmFsIl0.';
$arr =parse_url($url);
$new = explode("&",$arr['query']);
$new1 = explode("=",$new[0]);
echo($new1[1] );
output
com.gamevil.projectn.global
Maybe this can help you:
$url = 'https://play.google.com/store/apps/details?id=com.gamevil.projectn.global&feature=featured-apps#?t=W251bGwsMSwyLDIwMywiY29tLmdhbWV2aWwucHJvamVjdG4uZ2xvYmFsIl0.';
preg_match("/id=(.*?)&/", $url, $matches);
$package_name = $matches[1];
echo $package_name;
preg_match will no find everything between id= and &.
But a better solution is to use parse_url to parse the url and this function will return the components of the url.

Remove "http://" from URL string

I am using a bit.ly shortener for my custom domain. It outputs http://shrt.dmn/abc123; however, I'd like it to just output shrt.dmn/abc123.
Here is my code.
//automatically create bit.ly url for wordpress widgets
function bitly()
{
//login information
$url = get_permalink(); //for wordpress permalink
$login = 'UserName'; //your bit.ly login
$apikey = 'API_KEY'; //add your bit.ly APIkey
$format = 'json'; //choose between json or xml
$version = '2.0.1';
//generate the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$apikey.'&format='.$format;
//fetch url
$response = file_get_contents($bitly);
//for json formating
if(strtolower($format) == 'json')
{
$json = #json_decode($response,true);
echo $json['results'][$url]['shortUrl'];
}
else //for xml formatting
{
$xml = simplexml_load_string($response);
echo 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
As long as it is supposed to be url and if there is http:// - then this solution is the simplest possible:
$url = str_replace('http://', '', $url);
Change your following line:
echo $json['results'][$url]['shortUrl'];
for this one:
echo substr( $json['results'][$url]['shortUrl'], 7);
You want to do a preg_replace.
$variable = preg_replace( '/http:\/\//', '', $variable ); (this is untested, so you might also need to escape the : character ).
you can also achieve the same effect with $variable = str_replace('http://', '', $variable )

Slug URL generation function overriding the Ç

I have this function above to create url slugs from posts title, the problem is that the ç characther is not being converted to c. It is actually being override by the function.
Example post title: Coração de Pelúcia
The slug generated: coraao-de-pelucia
How can i fix this function to generate the slug like: coracao-de-pelucia
function generate_seo_link($input,$replace = '-',$remove_words = true,$words_array = array())
{
//make it lowercase, remove punctuation, remove multiple/leading/ending spaces
$return = trim(ereg_replace(' +',' ',preg_replace('/[^a-zA-Z0-9\s]/','',strtolower($input))));
//remove words, if not helpful to seo
//i like my defaults list in remove_words(), so I wont pass that array
if($remove_words) { $return = remove_words($return,$replace,$words_array); }
//convert the spaces to whatever the user wants
//usually a dash or underscore..
//...then return the value.
return str_replace(' ',$replace,$return);
}
You should use the iconv module and a function such as this one to do the conversion:
function url_safe($string){
$url = $string;
setlocale(LC_ALL, 'pt_BR'); // change to the one of your language
$url = iconv("UTF-8", "ASCII//TRANSLIT", $url);
$url = preg_replace('~[^\\pL0-9_]+~u', '-', $url);
$url = trim($url, "-");
$url = strtolower($url);
return $url;
}

preg_match and preg_replace for youtube url

i have this code
preg_match_all('%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $asd, $match);
to find youtube key of urls like
http://www.youtube.com/watch?v=ZiJRPREeQ1Q
<br />
http://www.youtube.com/watch?v=GaEZgqxPHLs&feature=related
this work good to find the code ZiJRPREeQ1Q and GaEZgqxPHLs , now i want to replace all the html line with a new code
wanna to use
preg_replace
to find the whole youtube url
*
to a new code how can i do that ?
--------------adds--------------
after i get the code of youtube from url by preg_math_all
i used this code to extract the codes
foreach($match[1] as $youtube){
// $youtube; // this handle the youtube code
$match = ""; // what can i write here relative to $youtube ?
$str .= preg_replace($match, 'new code',$content); // $content handle the whole thread that contain the youtube url <a href=*>*</a>
}
the only thing that i need that what's regular expression that i can use to replace youtube code
$html = file_get_contents($url); //or curl function
$re="<link itemprop=\"embedURL\" href=\"(.+)\">";
preg_match_all("/$re/siU", $html, $matches);
$youtube = $matches[1][0];
or
$html = file_get_contents($url); //or curl function
$re="<link itemprop=\"url\" href=\"(.+)\">";
preg_match_all("/$re/siU", $html, $matches);
$youtube = $matches[1][0];

Detection - the title of the URL and the URL

How to detect, if there is any URL in the text and title it has (if any)?
If there is one, then it should change the URL:
from: http://stackoverflow.com
into:
<detected:url="http://stackoverflow.com"/>
I need also to retrieve titles from external links like this example:
<title:http://stackoverflow.com/="the actual title from the stackoverflow"/>
This is for single URL case:
$url = "http://www.stackoverflow.com/";
$check_result = get_detected_and_title( $url );
function get_detected_and_title( $url )
{
$detected = '<detected:url="'.$url.'"/>';
$title = '';
$tmp_html = file_get_contents( $url );
preg_match('/<title>(.*)<\/title>/', $tmp_html, $res);
$title = '<title:'.$url.'="'.$res[1].'"/>';
return array( $detected, $title );
}
Actually, after looking through SO's pages, I think this is more close to what you looking for. Although it needs some adjustment: How to mimic StackOverflow Auto-Link Behavior

Categories