Regex extract part of a link (php) - php

I got some link like:
/3/topic/video1148288/
and I want to take the number after video. I can't replace the link with only numbers because there are more before the actual video's id.
I tried
$embed = preg_match("#\b/video([0-9][0-9][0-9][0-9][0-9][0-9][0-9])/#", $raw);
But it doesn't work.
Any help?

Give this a try:
$raw = "/3/topic/video1148288/";
preg_match("#/video(\d+)/#", $raw, $matches);
$embed = $matches[1];
Working example: http://3v4l.org/oLPMX
One thing to note from looking at your attempt, is that preg_match returns a truthy/falsely value, not the actual matches. Those are found in the third param ($matches in my example).

$raw = "/3/topic/video1148288/";
preg_match("/video(\d+)/", $raw, $results);
print "$results[1]";

preg_match('/(?<=video)\d+/i', $raw, $match);
echo $match[0];

Related

PHP How can I get full image url by useing preg_match_all

for example
$string =
'aaaaaaasssssshttp://www.1.com/images/001/001/001/1.jpgddwxeaaaaaa
crewcwehttp://www.2.com/images/002/002/002/2.jpgrcegcger
cgwcgrewhttp://www.3.com/images/003/003/003/3.jpgcgergcer
cerggewrcgrewhttp://www.4.com/images/004/004/004/4.jpgecgewrcgerwcg
cerghttp://www.5.com/images/005/005/005/5.jpg';
I want to get the first image full url form this stiring like :
"http://www.1.com/images/001/001/001/1.jpg"
I use this function but it does not work
if (preg_match_all('^http:\/\/\.(png|jpeg|jpg|gif|bmp)$/i', $string, $matches))
print_r ($matches[1]);
Who can help me ? thank you very much!
I've tested the following code, and it works. It only captures jpg-urls.
$string =
'aaaaaaasssssshttp://www.1.com/images/001/001/001/1.jpgddwxeaaaaaa
crewcwehttp://www.2.com/images/002/002/002/2.jpgrcegcger
cgwcgrewhttp://www.3.com/images/003/003/003/3.jpgcgergcer
cerggewrcgrewhttp://www.4.com/images/004/004/004/4.jpgecgewrcgerwcg
cerghttp://www.5.com/images/005/005/005/5.jpg';
preg_match_all("((http|https|ftp|ftps)://?([a-zA-Z0-9\\\./]*.jpg))", $string, $matches);
print_r($matches[0][0]);

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.

Extracting a string between an "id_" and a ".html"

I want to extract the id from this Youku video:
http://v.youku.com/v_show/id_XNTU2NzQyNzQ0.html?f=19275195&ev=3
The id is the random letter between the id_ and .html
How to accomplish that?
Use this
$input = 'http://v.youku.com/v_show/id_XNTU2NzQyNzQ0.html?f=19275195&ev=3';
preg_match('~id_(.*?).html~', $input, $output);
echo $output[1];
Output
XNTU2NzQyNzQ0
Codepad
You can try below code:
<?php
$varStr = 'http://v.youku.com/v_show/id_XNTU2NzQyNzQ0.html?f=19275195&ev=3';
$filename = basename($varStr);
preg_match_all('/id_(.*)\.html/', $filename, $match);
echo $match[1][0];
?>
Just for the sake of using named results in your REGEX, I would recommend doing something like this. Everyone else's work just fine, I've just added the named grouping as well as a non-greedy approach by ignoring periods
<?
$regex = "/\id_(?P<video_id>[^\.]*)\./";
if(preg_match($regex, "http://v.youku.com/v_show/id_XNTU2NzQyNzQ0.html?f=19275195&ev=3", $matches)) {
echo $matches['video_id'];
}

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

Regex to get ID of pastebin.com

I need to get the ID part of a pastebin link,
which is setup like http://pastebin.com/{id}, i have tired alot of different regex i am also using preg_match in php
preg_match("~http://pastebin.com/([0-9a-zA-Z]+)~", $url, $match);
print_r($match);
or
$url = "http://pastebin.com/a65d46";
$parsed = parse_url($url);
echo trim($parsed['path'])." is ID you needed";
Instead of regex, try using parse_url to extract the path
regex would be overkill for this.
$url = "http://pastebin.com/Ugj1eqCN"
$pos = strpos($url,"pastebin.com/");
echo substr($url,$pos+13);

Categories