regex pattern to match a url in php - php

i need help with this regex pattern. I have tried many different patterns but none return anything. I always get an empty array. The following patterns return no results.
//test 1
$regex = '/linkDestUrl = \'(.*)\'/';
//test 2
$regex = '#^(?:\s)*(linkDestUrl = \'(.*)\');#mi';
to match this
linkDestUrl = 'http://www.google.com';

if its that simple, this should work:
$search = "linkDestUrl = 'http://www.google.com';";
preg_match_all("/linkDestUrl = '(.*)';/im", $search, $result);
var_dump($result[1]);
Try it out on http://regex.larsolavtorvik.com/

You can use a site like http://gskinner.com/RegExr/ to test your reg exp

Related

How to check specyfic url_path in preg_match

How can I check if specific path match to pattern.
Example:
I have a path with one or more unknown variable
$pathPattern = 'user/?/stats';
And let say I received this path
$receivedPath = 'user/12/stats'
So, how can I check if that received path match to my pattern?
I tried to do something like below but didn't work.
$pathPattern = 'user/?/stats';
$receivedPath = 'user/12/stats';
$pathPatternReg = str_replace('?','.*',$pathPattern);
echo preg_match('/$pathPatternReg/', $receivedPath);
Thank you.
Regex should be something like this for a unknown user\/[0-9]+\/stats
And Could be used as such;
if(preg_match("user\/[0-9]+\/stats",$variable)) { .... }
As stated by Tom, you possibly have to escape the '/' characters with a '\'.
You only want to match one specific part of your total query string, the number in the center. Most regex interpreters provide this functionality in form of round brackets, like this:
$pattern = "user\/([0-9]+)\/stats";
Notice the round brackets around the [0-9]+ : it tells preg_match to store this part of the matched pattern in the $matches array.
So, your code could look like this:
$subject = "user/12/stats";
$pattern = "user\/([0-9]+)\/stats";
$matches = array();
if( preg_match($pattern, $subject, $matches) ){
// there was a match
// The $matches array now looks like this:
// { "user/12/stats", "12" }
// { <whole matched string>, <string in first parenthesis>, .... }
$user_id = $matches[1]
...
}
(not tested)
See also here: https://secure.php.net/manual/en/function.preg-match.php
Thank you booth.
This is a solution:
$pathPattern = 'user/?/stats';
$receivedPath = 'user/12/stats';
$pathPatternReg = str_replace(['/','?'],['\/','.*'],$pathPattern);
$pattern = "/^$uri/";
echo preg_match($pattern, $receivedPath);

PHP regex replace from string to string

I want to replace a section of a string based that starts with one string and ends with another, and I want the section between also replaced. I think this is possible using regex but I cant' seem to find any decent examples showing this.
For Example:
I have "http://www.website.com" and I want to replace from "www" to "com" with "123xyz".
So"http://www.website.com/something" becomes "http://123xyz/something.
I am assuming I have to use preg_replace(), and I think the regex should start with "^www" and end with "com$", but I cant seem to get a grasp of the syntax of regex enough to create the desired effect.
please help
With reference to your example , you can try like this
$string = 'http://www.website.com/something';
$pattern = '/www(.*)com/';
$replacement = '123xyz';
echo preg_replace($pattern, $replacement, $string);
$phrase = "http://www.website.com";
$phraseWords = array("www", "com");
$replaceTo = array("123xyz", "something");
$result = str_replace($phraseWords, $replaceTo, $phrase);
echo $result;
Thanks so much to both #CodingAnt and #PHPWeblineindia for your great answers. Using #CodingAnt's answer (and some more research I did online) I wrote this function:
function replaceBetween(&$target, $from, $to, $with){
if(strpos($target, $from)===false)return false;
$regex = "'".$from."(.*?)".$to."'si";
preg_match_all($regex, $target, $match);
$match = $match[1];
foreach($match as $m) $target = str_replace($from.$m.$to, $with, $target);
return $target;
}
It seems to work pretty well. I hope someone finds this useful.

Very simple regex to get the numbers in url: http://artige.no/bilde/6908

This is just a really simple regex-question. I'd like to grab the last numbers from a url that looks like this:
http://artige.no/bilde/6908
The url will always look like this, with a number after /bilde/
You do not really need a regex for this.
$url = 'http://artige.no/bilde/6908';
$num = intval(substr($url, strrpos($url, '/') + 1));
echo $num;
In which case,
~/bilde/(\d+)~
Is what you seek. Will find any number of digits after the string /bilde/
$matches = array();
$url = "http://artige.no/bilde/6908";
preg_match('/([0-9]+)$/', $url, $matches);
print_r($matches);
This uses the pattern ([0-9]+)$ to match one or more digits at the end of the string.
"(?<=/bilde/)\d+$"
test with grep:
kent$ echo "http://artige.no/bilde/6908"|grep -Po "(?<=/bilde/)\d+$"
6908
as you required, (/bilde/) url like:
http://artige.no/NoTWithBilde/1234
will not be matched.

Parsing a Source With REGEX

I want to get all Performance ID's from this page .
<?php
$content = file_get_contents("http://www124.popmundo.com/Common/Performances.asp?action=ComingPerformances&ArtistID=1962457");
$regex = "Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)";
//$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/";
//$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/s";
//all pattern variations tested, not working
if(preg_match_all($regex, $content, $m))
print_r($m);
else
echo "FALSE";
// this is returning FALSE
Use & instead of & in your regex.
Try this:
$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/";
It looks like an escape problem. Not knowing php, I would guess one of these
might fix it:
$regex = 'Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)';
or
$regex = "Performances\\.asp\\?action=Arrangements&PerformanceID=([0-9]+)";
or
$regex = '/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/';

PHP preg_match between text and the first occurrence of -

I'm trying to grab the 12345 out of the following URL using preg_match.
$url = "http://www.somesite.com/directory/12345-this-is-the-rest-of-the-url.html";
$beg = "http://www.somesite.com/directory/";
$close = "\-";
preg_match("($beg(.*)$close)", $url, $matches);
I have tried multiple combinations of . * ? \b
Does anyone know how to extract 12345 out of the URL with preg_match?
Two things, first off, you need preg_quote and you also need delimiters. Using your construction method:
$url = "http://www.somesite.com/directory/12345-this-is-the-rest-of-the-url.html";
$beg = preg_quote("http://www.somesite.com/directory/", '/');
$close = preg_quote("-", '/');
preg_match("/($beg(.*?)$close)/", $url, $matches);
But, I would write the query slightly differently:
preg_match('/directory\/(\d+)-/i', $url, $match);
It only matches the directory part, is far more readable, and ensures that you only get digits back (no strings)
This doesn't use preg_match but would achieve the same thing and would execute faster:
$url = "http://www.somesite.com/directory/12345-this-is-the-rest-of-the-url.html";
$url_segments = explode("/", $url);
$last_segment = array_pop($url_segments);
list($id) = explode("-", $last_segment);
echo $id; // Prints 12345
Too slow, I am ^^.
Well, if you are not stuck on preg_match, here is a fast and readable alternative:
$num = (int)substr($url, strlen($beg));
(looking at your code I guessed, that the number you are looking for is a numeric id is it is typical for urls looking like that and will not be "12abc" or anything else.)

Categories