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

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'];
}

Related

PHP preg_match and regular expression

I'm new for PHP
I am trying to get topic number of link but not work.
echo $topicsave is empty.
This my code.
$data = '
test_curl
';
preg_match_all('/\<a[^\?]+\/([^\"]+)\.\s*\>test_curl\<\/a\>/', $data, $match);
echo '<pre>',htmlspecialchars(print_r($match, true)),'</pre>';
if( count($match[0])){
foreach($match[1] as $vl){
preg_match_all('/topic\,([0-9]+\.[0-9]+)/', $vl, $m1);
if(count($m1[1]))
$topicsave = $m1[1][0];
echo $topicsave;
}
}
I want to get topic number 40500 please help me, topic is variable such as 120 or 2536 or 12456.
Thank you.
To extract the topic number from link you can use following regex.
Regex: topic,(\d+(\.\d+)*)\.html
Explanation: What am doing is feeding your link to regex and extracting number between topic, and .html.
Regex101 Demo
PHP demo on Ideone
You can do it with this:
$re = "/topic,(?'topic'\\d+)/";
$str = "test_curl";
preg_match($re, $str, $matches);
echo $matches['topic'];
Which will output:
40500
What I used here (?'topic'\\d+) is a named group. It allows you to retrieve data from your matches with the name you used (here topic).
If you need to do live tests, Regex 101 is great.
Try this solution:
$data = 'test_curl';
preg_match_all('/topic,(.*?)\..*\.html/s', $data, $match);
echo $match[1][0]; // Output: 40500

How to make the first letter of specific array in PHP?

I have a simple regex that trims urls to their root domain.
Problem: How to make the first letter of a specific array in PHP ? The array output is an associative array. The line echo $matches[0] is the output that I need to convert the first letter to capitalize.
<?php
$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
$url = 'http://www.test.com.uk';
//echo $url;
if (preg_match($pattern, $url, $matches) === 1) {
echo $matches[0];
}
?>
The code works okay except that the associative array must have a capitalize letter
The output of the code above looks like this: test.com.uk
Output: But the output I am looking for is this: Test.com.uk
Please help me.
Use ucfirst() on your match:
echo ucfirst($matches[0]);
Use the ucfirst function:
echo ucfirst($matches[0]);
Just use the ucfirst php function
echo ucfirst($matches[0]);
Might be more appropriate instead of regex:
echo ucfirst(parse_url($url, PHP_URL_HOST));

Regex extract part of a link (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];

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);

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]+)/';

Categories