PHP Regex Matching Image URLs - php

This is my Image Url PHP Code.
$GetImage = 'https://lh6.ggpht.com/hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw=w124';
preg_match_all("/https://\w\w\d.\w+.com/[\w-]+=\w\d{2,3}/", $GetImage, $Result, PREG_SET_ORDER);
its working for me, but i want to extract "[\w-]" pattern results, in other words, i want to extract "hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw" this string from my image Url...
Please anybody help my to solve this problem....
thanks

I feel it's overkill to try to match the entire URL using a regular expression. I suggest you parse the URL first using PHP's built-in function parse_url().
<?php
$str = 'https://lh6.ggpht.com/hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw=w124';
// Parse the URL before applying a regex. Only get the path part. Use substring to remove the leading slash
$path = substr( parse_url( $str, PHP_URL_PATH ), 1 );
$pattern = '/([^=]+)/';
$matches = array();
if ( preg_match( $pattern, $path, $matches ) ) {
// Regex matched
$id = $matches[1];
// Outputs: string 'hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw' (length=66)
var_dump( $id );
}
?>
Note that the snippet does not check the domain name. You can easily adjust the script to do so by not limiting the parse_url() function to only return the path, but also the other parts.

Try like this
$GetImage = 'https://lh6.ggpht.com/hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw=w124';
preg_match_all('#https://.*\.com/([\w-]+=\w\d{2,3})#iU', $GetImage, $match, PREG_SET_ORDER);
print_r($match);

Related

SPLIT URL in PHP

I have below URL in my code and i want to split it and get the number from it
For example from the below URL need to fetch 123456
https://review-test.com/#/c/123456/
I have tried this and it is not working
$completeURL = https://review-test.com/#/c/123456/ ;
list($url, $number) = explode('#c', preg_replace('/^.*\/+/', '', $completeURL));
Use parse_url
It's specifically made for this sort of thing.
You can do this without using regex also -
$completeURL = 'https://review-test.com/#/c/123456/' ;
list($url, $number) = explode('#c', str_replace('/', '', $completeURL));
echo $number;
If you wan to get the /c/123456/ params you will need to execute the following:
$url = 'https://review-test.com/#/c/123456/';
$url_fragment = parse_url($url, PHP_URL_FRAGMENT);
$fragments = explode('/', $url_fragment);
$fragments = array_filter(array_map('trim', $fragments));
$fragments = array_values($fragments);
The PHP_URL_FRAGMENT will return a component of the url after #
After parse_url you will end up with a string like this: '/c/123456/'
The explode('/', $url_fragment); function will return an array with empty indexes where '/' was extracted
In order to remove empty indexes array_filter($fragments); the
array_map with trim option will remove excess spaces. It does not
apply in this case but in real case scenario you better trim.
Now if you var_dump the result you can see that the array needs to
be reindexed array_values($fragments)
You should try this: basename
basename — Returns trailing name component of path
<?php
echo basename("https://review-test.com/#/c/123456/");
?>
Demo : http://codepad.org/9Ah83qaP
Subsequently you can directly take from pure regex to fetch numbers from string,
preg_match('!\d+!', "https://review-test.com/#/c/123456/", $matches);
print_r($matches);
Working demo
Simply:
$tmp = explode( '/', $completeUrl).end();
It will explode the string by '/' and take the last element
If you have no other option than regex, for your example data you could use preg_match to split your url instead of preg_replace.
An approach could be to
Capture the first part as a group (.+\/)
Then capture your number as a group (\d+)
Followed by a forward slash at the end of the line \/$/
This will take the last number from the url followed by a forward slash.
Then you could use list and skip the first item of the $matches array because that will contain the text that matched the full pattern.
$completeURL = "https://review-test.com/#/c/123456/";
preg_match('/(.+\/)(\d+)\/$/', $completeURL, $matches);
list(, $url, $number) = $matches;

Simple str_replace() making things wrong - WordPress

I need some special filtering to certain text all over my website, like below:
function special_text( $content ) {
$search_for = 'specialtext';
$replace_with = '<span class="special-text"><strong>special</strong>text</span>';
return str_replace( $search_for, $replace_with, $content );
}
add_filter('the_content', 'special_text', 99);
It's doing thing in an excellent way, BUT...
in content if there's any link like: <a title="specialtext" href="http://specialtext.com">specialtext</a> then the title and href texts also changed and the link becomes broken.
How can I make exception there?
Is there a way I can put some exceptions in an array and str_replace() simply skip 'em?
You should use regular expression and use function preg_replace() to replace matched string. Here is the full implementation of your special_text() function.
function special_text( $content ) {
$search_for = 'specialtext';
$replace_with = '<span class="special-text"><strong>special</strong>text</span>';
return preg_replace( '/<a.*?>(*SKIP)(*F)|'.$search_for.'/m', $replace_with, $content );
}
In the following regular expression first, using <a.*?> - everything between <a...> is matched and using (*SKIP)(*F)| it is skipped and then from anything else $search_for is matched (in your case it's specialtext).
Jezzabeanz quite got it except you can simplify it still with:
return preg_replace("/^def/", $replace_with, $content);
If you just want to change the text between the the a tags then a regular expression works wonders.
Here is something I used when I was pulling data from emails sent to me:
(?<=">)(.*?\w)(?=<\/a)
returns "specialtext"
It also returns "specialtext test" if there is whitespace.
Regular expressions are definitely the way to go.
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
Source
And then do a replace on the returned matches.

This script won't find Absolute Urls

in the code below, it is supposed to scan links and index them in the array [links]. but for some reason, they won't index.
I am starting to think if my regex code is wrong, how can i improve it. Also is it my file_get_contents command? Is it used correctly?
$links = Array();
$URL = 'http://www.theqlick.com'; // change it for urls to grab
// grabs the urls from URL
$file = file_get_contents($URL);
$abs_url = preg_match_all("'^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$^'", $file, $link);
if (!empty($abs_url)) {
$links[] = $abs_url;
}
In your preg_match_all you are saving into $link not $links.
preg_match_all Returns the number of full pattern matches (which might be zero), or FALSE if an error occurred (c) php.net
preg_match_all("'^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$^'", $file, $matches);
if (!empty($matches)
$links = $matches;
Your regex is wrong. You have a head anchor ^ at the end of the pattern adjacent to a tail match $. I don't think the anchors really aren't needed. Additionally, your variable you are storing matches in $link (no s). Plus your pattern delimiter appears to be the ' character. Was that intentional? It would fortunately work, but I'm guessing you didn't intend for that?
Try this:
$matchCount = preg_match_all("/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/", $file, $matches);
if ($matchCount)
{
foreach ($matches as $match)
{
$links[] = $match[0];
}
}
Read up on PHP regular expressions.

Need a reg expression to get numbers from url using preg_match_all

function strip_cdata($string)
{
preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $string, $matches);
return str_replace($matches[0], $matches[1], $string);
}
I use the above function to extract text from cdata, but I don't know how to modify the reg expression to get a number from the url below.
http://blah.somesite.com/anytextcouldbehere/2828842087.html
Specifically, I need to get "2828842087" from the above url. It will always be numbers and be between the last "/" and ".html".
Thanks
No need to use regexp here:
$url = 'http://blah.somesite.com/anytextcouldbehere/2828842087.html';
$data = parse_url($url);
$number = basename($data['path'], '.html');
Here's the regular expression:
$url = 'http://blah.somesite.com/anytextcouldbehere/2828842087.html';
if( preg_match('#/(\d+)\.html$#', $url, $matches) ){
$number = $matches[1];
}
You can also try this one instead of regex:
$url = 'http://blah.somesite.com/anytextcouldbehere/2828842087.html';
$info = pathinfo($url);
$num = $info['filename'];
echo $num;
use this regex
/\d*/
preg_match_all('/\d*/is', $string, $matches);
you can use this:
preg_match_all('/^http\:\/\/([\w\/\.]+)\/(<?P<num>[\d]+)\.html$/',$url,$matches);
now the number is in $matches['num']. that is an array like this:
Array(0=>2828842087)
good luck

PHP preg_replace question?

hey guys,
I'm a preg_replace noob and don't understand how to solve the following case:
$youtubeurl = "((http|https)\:\/\/(www|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)\.youtube\.(com|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)([a-zA-Z0-9\-\.\/\?_=&;]*))";
$content = preg_replace($youtubeurl, embedCode($youtubeurl), $content);
I have a pattern that matches any youtube URL.
If this pattern is matched i want to call the function embedCode() and pass along the matched string.
How can i do this. right now i'm obviously passing along the regexp code which is of course wrong. I need to pass along the matched string.
thank you
Try preg_replace_callback.
$youtube_url_pattern = "#((http|https)\:\/\/(www|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)\.youtube\.(com|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)([a-zA-Z0-9\-\.\/\?_=&;]*))#";
if(preg_match($youtube_url_pattern, $content_containing_url, $content)){
embedCore($contet[index]);
}
You just have to find the index of matched url, try print_r($content); inside the if and see what is the index for the matched pattern.
You are trying to do this:
if ( preg_match( '/' . $youtubeurl . '/', $content, $match ) )
{
embedCode( $match );
}

Categories