I have the following string
$string = "Hello World!<br />- 8/7/2013<br />Content<br />- 8/6/2013<br />Hello World";
I want to extract all the information between the dash + space ("- ") and the next line break tag. Is that possible? I've been researching Google for hours but no luck. I'm thinking that I need an array of strpos of the "- " and the following line break tag and then batch substr them. But of course if you can do this any other way, that would be so much appreciated!
I've updated my answer to handle multiple occurances.
You can do this with a simple regular expression:
preg_match_all("#- (.*)<br />#U", $string, $matches);
print_r($matches[1]);
The above will print
Array
(
[0] => 8/7/2013
[1] => 8/6/2013
)
This matches the pattern - (.*)<br /> with (.*) meaning anything. The #s here work as delimiters to separate the actual pattern with the modifiers (in this case U meaning an ungreedy match).
regular expressions will give you the most control, but here is a quick demo using strpos and substr to get what you asked for:
$strStart = strpos($string, '- ');
$strLength = strpos($string, '<br') - $strStart;
substr( $string, $strStart, $strLength );
Related
I want to use the regex:
/(.*)[.\s][sS](\d{1,20})[eE](\d{1,100}).*/i
to filter for the title of a tv series. (e.g. The Big Bang Theory S04E05) In order to remove the episode string (S04E05).
I've tested my regex with http://www.phpliveregex.com/ and everything works fine. But including it to my website, I'll get the whole title including the episode string.
The return value of preg_match is 0.
My Code:
$ret=preg_match("/(.*)[.\s][sS](\d{1,20})[eE](\d{1,100}).*/i", $title,$output);
if($ret==1){
$title_without=$output[1];
}
Note that inside a double-quoted string, you need to use double backslash to escape regex shorthand classes.
You can use your regex inside a preg_replace function inside single quotes so that you do not have to double backslashes:
$title= "The Big Bang Theory S04E05";
$ret=preg_replace('/^(.*)[.\s]s\d{1,20}e\d{1,100}(.*)/i', '\1\2', $title);
echo $ret;
See IDEONE demo. Result: The Big Bang Theory.
The back-references \1\2 will restore the substrings before and after the episode substring.
Since you are using /i modifier, you need not use [eE] or [Ss], just use single letters in any case.
To return the substring before the episode and the episode substring itself, just use the capturing groups with preg_match like here:
$title= "The Big Bang Theory S04E05";
$ret=preg_match('/^(.*)[.\s](s\d{1,20}e\d{1,100})/i', $title, $match);
echo $match[1] . PHP_EOL; // => The Big Bang Theory
echo $match[2]; // => S04E05
See another demo
You could look for words and match all but the last one:
$matches = array();
$regex = "/^([\w ]*) [\w]+$/i";
$title = "The Big Bang Theory S04E05";
preg_match_all ($regex, $title, $matches);
Now all your matches are in $matches.
Here I have a string, "Hello World! I am trying out regex in PHP!". What I want to do is retrieve string values between a set of characters. In this example, the characters are ** **
$str = "**Hello World!** I am trying out regex in PHP!";
preg_match('#\*\*(.*)\*\*#Us', $str, $match);
echo $match[1];
This will echo out "Hello World!", but I want to echo out several matches:
$str = "**Hello World!** I am trying out **regex in PHP!**";
How would I be able to do so? I tried using preg_match_all() but I don't think I was using it properly, or that it would work at all in this case.
You can use:
$str = "**Hello World!** I am trying out **regex in PHP!**";
preg_match_all('/\*{2}([^*]*)\*{2}/', $str, $m);
print_r($m[1]);
Array
(
[0] => Hello World!
[1] => regex in PHP!
)
Even your regex #\*\*(.*)\*\*#Us should work with this but my suggested regex is little more efficient due to negation based pattern [^*]*
You got 1 match owing to using preg_match.You should use preg_match_all Here is another pattern.It uses word non word match between the delimiters
<?php
$str = "**Hello World!** I am trying out **regex in PHP!**";
$regex='/\*\*([\w\W]*)\*\*/iU';
preg_match_all($regex, $str, $m);
print_r($m[1]);
I suggest you to use a non-greedy form of regex. Because i think you want to match also the contents (text inside **) where the single * resides.
$str = "**Hello World!** I am trying out **regex in PHP!**";
preg_match_all('~\*\*(.*?)\*\*~', $str, $matches);
print_r($matches[1]);
DEMO
Lets say I have expression like
$string = ( score + total-score - total-min_score) / papoy
I want to able to extract those ALL words/term into an array ( words with/without dash and underscore )
I tried like (I'm not so good with regex)
preg_match("(\w+-_)",$string,$matches);
But it only return me the first match. How could I get all matches?
You need to use preg_match_all function.
preg_match_all('~[\w-]+~',$string,$matches);
OR
preg_match_all('~\w+(?:-\w+)*~', $string, $matches);
DEMO
I would like to know how I can cut a string in PHP starting from the last character -> to a specific character. Lets say I have following link:
www.whatever.com/url/otherurl/2535834
and I want to get 2535834
Important note: the number can have a different length, which is why I want to cut out to the / no matter how many numbers there are.
Thanks
In this special case, an url, use basename() :
echo basename('www.whatever.com/url/otherurl/2535834');
A more general solution would be preg_replace(), like this:
<----- the delimiter which separates the search string from the remaining part of the string
echo preg_replace('#.*/#', '', $url);
The pattern '#.*/#' makes usage of the default greediness of the PCRE regex engine - meaning it will match as many chars as possible and will therefore consume /abc/123/xyz/ instead of just /abc/ when matching the pattern.
Use
explode() AND end()
<?php
$str = 'www.whatever.com/url/otherurl/2535834';
$tmp = explode('/', $str);
echo end ($tmp);
?>
Working Demo
This should work for you:
(So you can get the number with or without a slash, if you need that)
<?php
$url = "www.whatever.com/url/otherurl/2535834";
preg_match("/\/(\d+)$/",$url,$matches);
print_r($matches);
?>
Output:
Array ( [0] => /2535834 [1] => 2535834 )
With strstr() and str_replace() in action
$str = 'www.whatever.com/url/otherurl/2535834';
echo str_replace("otherurl/", "", strstr($str, "otherurl/"));
strstr() finds everything (including the needle) after the needle and the needle gets replaced by "" using str_replace()
if your pattern is fixed you can always do:
$str = 'www.whatever.com/url/otherurl/2535834';
$tmp = explode('/', $str);
echo $temp[3];
Here's mine version:
$string = "www.whatever.com/url/otherurl/2535834";
echo substr($string, strrpos($string, "/") + 1, strlen($string));
I want to search a phone number from a whole sentence. It can be any number with a pattern like (122) 221-2172 or 122-221-2172 or (122)-221-2172 by help of PHP where I don't know in which part of the sentence that number is exists or I could use substr.
$text = 'foofoo 122-221-2172 barbar 122 2212172 foofoo ';
$text .= ' 122 221 2172 barbar 1222212172 foofoo 122-221-2172';
$matches = array();
// returns all results in array $matches
preg_match_all('/[0-9]{3}[\-][0-9]{6}|[0-9]{3}[\s][0-9]{6}|[0-9]{3}[\s][0-9]{3}[\s][0-9]{4}|[0-9]{9}|[0-9]{3}[\-][0-9]{3}[\-][0-9]{4}/', $text, $matches);
$matches = $matches[0];
var_dump($matches);
You can use regular expressions to solve this. Not 100% on php syntax, but I imagine it would look something like:
$pattern = '/^\(?\d{3}\)?-\d{3}-\d{4}/';
^ says "begins with"
\( escapes the (
\(? say 0 or 1 (
\d{x} says exactly x numbers
You may also want to check out Using Regular Expressions with PHP