Getting id from url using regex - php

http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567
http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567%2Fsubtitle
http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F123456
http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F123456%2Fsubtitle
http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567%2F
http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F123456%2F
The URL's always start with:
http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F
The ids are always numeric, however the number of digits can vary.
How to get the id (1234567 and 123456) from above sample URL's?
I've tried using the following pattern without luck (it doesn't return any matches):
/^http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F(\d)$/

I would recommend you to first parse this url and extract the url query string parameter and url decoding it:
function getParameterByName(url, name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(url);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
like this:
var url = 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567';
var p = getParameterByName(url, 'url');
and then use some regex to parse p and extract the necessary information like /\d+/.

With proper URL parsing functions you can do this:
parse_str(parse_url($url, PHP_URL_QUERY), $params);
if (isset($params['url'])) {
parse_str(parse_url($params['url'], PHP_URL_QUERY), $params);
if (isset($params['movie'])) {
$movie = $params['movie'];
}
}

$urls = array(
'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567'
, 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567%2Fsubtitle'
, 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F123456'
, 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F123456%2Fsubtitle'
, 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567%2F'
, 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F123456%2F'
);
foreach ($urls as $url) {
if (preg_match('/%2Fmovie%2F(\d+)/', $url, $matches)) {
var_dump($matches[1]);
}
}
KISS. I was originally going to use parse_url(), but there is no way to parse a query string without regular expressions anyway.

There's a way without parsing too. Assuming $url = URL
http://codepad.org/t91DK9H2
$url = "http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567%2Fsubtitle";
$reg = "/^([\w\d\.:]+).*movie%2F(\d+).*/";
$id = preg_replace($reg,"$2",$url);

It looks likes you need to escape some special characters.
try:
/^http://example.com/movie.swf\?url=http%3A%2F%2Fexample.com%2Fmovie%2F(\d+)$/

Related

preg_match, addslashes,mb_substr not working for long strings

I am parsing an html file. I have a big string which is basically a script.
The string looks likes this:
var spConfig = new
Product.Config({"outofstock":["12663"],"instock":["12654","12655","12656","12657","12658","12659","12660","12661","12662","12664","12665"],"attributes":{"698":{"id":"698","code":"aubade_import_colorcode","label":"Colorcode","options":[{"id":"650","label":"BLUSH","price":"0","products":["12654","12655","12656","12657","12658","12659","12660","12661","12662","12663","12664","12665"]}]},"689":{"id":"689","code":"aubade_import_size_width","label":"Size
Width","options":[{"id":"449","label":"85","price":"0","products":["12654","12657","12660","12663"]},{"id":"450","label":"90","price":"0","products":["12655","12658","12661","12664"]},{"id":"451","label":"95","price":"0","products":["12656","12659","12662","12665"]}]},"702":{"id":"702","code":"aubade_import_size_cup","label":"Size
Cup","options":[{"id":"1501","label":"A","price":"0","products":["12654","12655","12656"]},{"id":"1502","label":"B","price":"0","products":["12657","12658","12659"]},{"id":"1503","label":"C","price":"0","products":["12660","12661","12662"]},{"id":"1504","label":"D","price":"0","products":["12663","12664","12665"]}]}},"template":"\u20ac#{price}","basePrice":"57","oldPrice":"57","productId":"12666","chooseText":"Choose
option...","taxConfig":{"includeTax":true,"showIncludeTax":true,"showBothPrices":false,"defaultTax":19.6,"currentTax":19.6,"inclTaxTitle":"Incl.
Tax"}});
var colorarray = new Array();
colorarray["c650"] = 'blush';
Event.observe('attribute698', 'change', function() {
var colorId = $('attribute698').value;
var attribute = 'attribute698';
var label = colorarray["c"+colorId];
if ($('attribute698').value != '') {
setImages(attribute, colorId, label);
}
}); // var currentColorLabel = 'blush'; // var currentSku = '5010-4-n'; // var currentPosition = 'v'; // //
Event.observe(window, 'load', function() { //
setImages('attribute698', null, currentColorLabel); // });
I need to extract the content from first "(" upto first ";".
I have tried to do string extract and failed.I have tried preg match I have failed.
Kindly tell me some solution to my problem.Below are my tried solution and issues.
$strScript = $tagscript->item(0)->nodeValue;
//this line returns empty string
$str_slashed = addslashes(trim($strScript) );
$pattern = '/\((.*);/';
preg_match($pattern,$str_slashed,$matches);
echo 'matches'."<br />";
var_dump($matches);
//Add slashes works only if I use it before assignment to other string
$matches = array();
$strScript = addslashes ($tagscript->item(0)->nodeValue);//. "<br />";
$pattern = '/\((.*);/';
preg_match($pattern,$strScript,$matches);
echo 'matches'."<br />";
var_dump($matches);
//str extract method
$posBracket = stripos ($strScript,'(');
echo $posBracket."<br />";
$posSemiColon = strpos ($strScript,';');
echo $posSemiColon."<br />";
$temp = mb_substr ($strScript,$posBracket ,($posSemiColon-$posBracket));
echo $temp."<br />";
The above code works for small strings
$strScript = "manisha( [is goo girl] {come(will miss u) \and \"play} ; lets go home;";
but wont work for the long strings.
How can i resolve this issue?Please help me!
You have to add multiline switch to your regular expressions.
Try $pattern = '/\((.*);/s'; or $pattern = '/\((.*);/m';
Try using /\(([^;]*)/ as your pattern. [^;] means any character that is not a ;.
Edit: also turn multiline mode on, as suggested by rogers; therefore the whole pattern should look somewhat like /\(([^;]*)/s.
Edit: you should be aware, that this is not really error-proof. Say, you'll get a ; inside some property of the object of which JSON representation is included in your string.

php count wordand remove

how to remove some word on url inp php?
URL : /hiaz/0.1.1/index.php?route=product/product&product_id=153&tracking=507b790fadc48
i just want :
tracking=507b790fadc48
If you have the full url stored in the var $myurl:
$result = substr($myurl, strrpos($myurl, "tracking"), strlen ($myurl) );
You can make use of $_GET if you are working in the index.php. Alternatively, if you wish to grab whole query string, you can use $_SERVER['QUERY_STRING']
What you have is
$url = /hiaz/0.1.1/index.php?route=product/product&product_id=153&tracking=507b790fadc48
What you want is
$newurl = /hiaz/0.1.1/index.php?tracking=507b790fadc48
First extract part of url upto "?" character.
Then extract the tracking part and combine.
$part1 = substr($url,0,strpos($url,'?'));
$part2 = substr($url,strpos($url,"tracking"),strlen($url)-strpos($url,"tracking"));
$newurl = $part1 + $part2;

Get value from URL after the last /

I have looked around for this but can only find links and references to this been done after an anchor hashtag but I need to get the value of the URL after the last / sign.
I have seen this used like this:
www.somesite.com/archive/some-post-or-article/53272
the last bit 53272 is a reference to an affiliate ID..
Thanks in advance folks.
PHPs parse_url (which extracts the path from the URL) combined with basename (which returns the last part) will solve this:
var_dump(basename(parse_url('http://www.somesite.com/archive/some-post-or-article/53272', PHP_URL_PATH)));
string(5) "53272"
You can do this :
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$id = substr(url, strrpos(url, '/') + 1);
You can do it in one line with explode() and array_pop() :
$url = 'www.somesite.com/archive/some-post-or-article/53272';
echo array_pop(explode('/',$url)); //echoes 53272
<?php
$url = "www.somesite.com/archive/some-post-or-article/53272";
$last = end(explode("/",$url));
echo $last;
?>
Use this.
I'm not an expert in PHP, but I would go for using the split function: http://php.net/manual/en/function.split.php
Use it to split a String representation of your URL with the '/' pattern, and it will return you an array of strings. You will be looking for the last element in the array.
This will work!
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$pieces = explode("/", $url);
$id = $pieces[count($pieces)]; //or $id = $pieces[count($pieces) - 1];
If you always have the id on the same place, and the actual link looks something like
http://www.somesite.com/archive/article-post-id/74355
$link = "http://www.somesite.com/archive/article-post-id/74355";
$string = explode('article-post-id/', $link);
$string[1]; // This is your id of the article :)
Hope it helped :)
$info = parse_url($yourUrl);
$result = '';
if( !empty($info['path']) )
{
$result = end(explode('/', $info['path']));
}
return $result;
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$parse = explode('/',$url);
$count = count($parse);
$yourValue = $parse[$count-1];
That's all.

Split the data in php

I have a variabe named $link in PHP that repeats itself and it has data like:
http://www.ankinfos.com/our-portfolio/www.abc.com
http://www.ankinfos.com/our-portfolio/www.cab.com
http://www.ankinfos.com/our-portfolio/www.zzz.com
.
.
.
.
http://www.ankinfos.com/our-portfolio/www.lal.com
I want to remove the text or data before www.abc.com so that it becomes
http://www.ankinfos.com/our-portfolio/www.abc.com
to
www.abc.com
and
http://www.ankinfos.com/our-portfolio/www.abc.com
to
www.cab.com
and so on
Please let me know any solution for that in jquery or php
I think the easiest way in php is to do
$url = "http://.../www.abc.com";
$url = explode('/', $url);
$yourUrl = $url[count($url) - 1];
As for javascript:
var url = "http://.../www.abc.com";
url = url.split("/");
var yourUrl = url[url.length - 1];
http://php.net/manual/en/function.explode.php
$myArray = explode("http://www.ankinfos.com/our-portfolio/", $link);
array_shift($myArray);
print_r($myArray);
This will explode each url (Which I assume is constant) and will remove it's first element (since the delimiter is at the start, it will cause an empty string to be placed as the first item).
Now, you can iterate the items.
Use split() function in javascript
var fullLink = "http://www.ankinfos.com/our-portfolio/www.abc.com";
var url = fullLink.split('/');
url = url[url.length-1]
alert(url)​
http://jsfiddle.net/Gsruv/
And explode() in PHP

Extract number from variable

I have this string:
$guid = 'http://www.test.com/?p=34';
How can I extract the value of get var p (34) from the string and have $guid2 = '34'?
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $vars);
$guid2 = $vars['p'];
If 34 is the only number in the query string, you can also use
echo filter_var('http://www.test.com/?p=34', FILTER_SANITIZE_NUMBER_INT); // 34
This will strip anything not a number from the URL string. However, this will fail the instant there is other numbers in the URL. The solution offered by konforce is the most reliable approach if you want to extract the value of the p param of the query string.
A preg_replace() is probably the quickest way to get that variable, the code below will work if it is always a number. Though konforce's solution is the general way of getting that information from a URL, though it does a lot of work for that particular URL, which is very simple and can be dealt with simply if it unaltering.
$guid = 'http://www.test.com/?p=34';
$guid2 = preg_replace("/^.*[&?;]p=(\d+).*$/", "$1", $guid);
Update
Note that if the URLs can not be guaranteed to have the variable p=<number> in them, then you would need to use match instead, as preg_replace() would end up not matching and returning the whole string.
$guid = 'http://www.test.com/?p=34';
$matches = array();
if (preg_match("/^.*[&?;]p=(\d+).*$/", $guid, $matches)) {
$guid2 = $matches[1];
} else {
$guid2 = false;
}
That is WordPress. On a single post page you can use get_the_ID() function (WP built-in, used in the loop only).
$guid2 = $_GET['p']
For more security:
if(isset($_GET['p']) && $_GET['p'] != ''){
$guid2 = $_GET['p'];
}
else{
$guid2 = '1'; //Home page number
}

Categories