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
Related
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.
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];
I have to extract the email from the following string:
$string = 'other_text_here to=<my.email#domain.fr> other_text_here <my.email#domain.fr> other_text_here';
The server send me logs and there i have this kind of format, how can i get the email into a variable without "to=<" and ">"?
Update: I've updated the question, seems like that email can be found many times in the string and the regular expresion won't work well with it.
You can try with a more restrictive Regex.
$string = 'other_text_here to=<my.email#domain.fr> other_text_here';
preg_match('/to=<([A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4})>/i', $string, $matches);
echo $matches[1];
Simple regular expression should be able to do it:
$string = 'other_text_here to=<my.email#domain.fr> other_text_here';
preg_match( "/\<(.*)\>/", $string, $r );
$email = $r[1];
When you echo $email, you get "my.email#domain.fr"
Try this:
<?php
$str = "The day is <tag> beautiful </tag> isn't it? ";
preg_match("'<tag>(.*?)</tag>'si", $str, $match);
$output = array_pop($match);
echo $output;
?>
output:
beautiful
Regular expression would be easy if you are certain the < and > aren't used anywhere else in the string:
if (preg_match_all('/<(.*?)>/', $string, $emails)) {
array_shift($emails); // Take the first match (the whole string) off the array
}
// $emails is now an array of emails if any exist in the string
The parentheses tell it to capture for the $matches array. The .* picks up any characters and the ? tells it to not be greedy, so the > isn't picked up with it.
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'];
}
I'm trying to extract the following in php, but my regex or egreg is out of place and i got really confused. Please help me put these to two variables:
<a onmouseover="dgsa.sm(this)" onmouseout="dgsa.hm();" href="http://www.cnn.com/testpage.html#page_mostview">test titles</a>
I want the variable:
$url="http://www.cnn.com/testpage.html#page_mostview";
$title="test titles";
Any kind of help on this is greatly appreciated.
If you have in one big string:
<?php
$str = 'Hello Bye';
$expr = '/<a[^>]*? href=\"(?<url>[^\"]+)\"[^>]*?>(?<text>.*?)<\/a>/';
echo preg_match_all($expr, $str, $amatches);
echo '<br><br>';
print_r($amatches);
?>
You can also use:
preg_match_all($expr, $str, $amatches, PREG_SET_ORDER);
Which may suit better (I ofen prefer this approach)
If you have in spearate strings and handle in a loop, use preg_match instead. Tis will only return the first, and you loop.
Edit: here it is with your example, as requested.
<?php
$str = '<a onmouseover="dgsa.sm(this)" onmouseout="dgsa.hm();" href="http://www.cnn.com/testpage.html#page_mostview">test titles</a>';
$expr = '/<a[^>]*? href=\"(?<url>[^\"]+)\"[^>]*?>(?<text>.*?)<\/a>/';
echo preg_match_all($expr, $str, $amatches, PREG_OFFSET_CAPTURE);
echo '<br><br>';
print_r($amatches);
?>