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);
?>
Related
I am getting a unique issue. I have string like "Home[||](|i am in here|)[||]". Now i want to replace everything after first occurance from left of '[||]' with null. Tried many ways like pregreplace,substring,explode,strstr,strreplace... But nothing seems to work out. Can someone please help.
Using strstr() would be about the simplest...
echo strstr("Home[||](|i am in here|)[||]", "[||]", true);
You can use regex to find the part of the string you want to replace.
<?php
$input = "Home[||](|i am in here|)[||]";
$pattern = "/\[\|\|\](.*)/";
$output = preg_replace($pattern, "", $input);
echo $output;
?>
You can do it by many way using strstr(), strtok(), preg_replace() etc. But I would prefer with explode(), without any kind of regex or str_* function to achieve what you want. Hope this will help to get your job done.
echo explode('[||]',"Home[||](|i am in here|)[||]")[0];
DEMO: https://eval.in/1040308
With strtok()
<?php
$mystring = 'Home[||](|i am in here|)[||]';
$first = strtok($mystring, '[||]');
echo $first; // Home
?>
With strstr():
<?php
$mystring = 'Home[||](|i am in here|)[||]';
$first = strstr($mystring, "[||]", true);
echo $first; // Home
?>
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
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 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]+)/';
So I am using a preg_match to get any text after a # up until a space out of a string. However if there are multiple occasions of it in the string it will only return the first one. This is what I have so far
$text = '#demo1 #demo2 some text #blah2';
$check_hash = preg_match("/([#][a-zA-Z-0-9]+)/", $text, $hashtweet);
foreach ($hashtweet as $ht){
echo $ht;
}
The echo $ht; outputs #demo1#demo1 when it should output all 3 of the words with # in front. Any help is greatly appreciated.
You want to use preg_match_all.
Example:
<?php
$text = '#demo1 #demo2 some text #blah2';
$check_hash = preg_match_all("/([#][a-zA-Z-0-9]+)/", $text, $hashtweet);
foreach ($hashtweet[1] as $ht){
echo $ht;
}
Check preg_match_all