I have this regular expression in php:
$programacion_array = preg_split("/(([0-9]{2}):([0-9]{2}))/",$html, -1, PREG_SPLIT_DELIM_CAPTURE);
I want to split a text by finding the hours of the programs, sample text:
asdf06:20Programold07:20Programnew
But my regular expression is returning me this:
[1] => 06:20 [2] => 06 [3] => 20 [4] => Programold
I don't see what I am missing.
Try getting rid of the inner match groups, e.g. use just:
/([0-9]{2}:[0-9]{2})/
This will work as shown here: http://ideone.com/u44OIt
Related
I am trying to parse a string that contain strings that are 9-11 characters long and are integers and starts with 08 or +62. How do I do this in PHP? Here's my regex thus far:
/^(\+?62|08)[0-9]{9,11}$/
so here's some sample string/integer I should be able to extract out of a long string
082298744807
087884962429
087783218768
0818809692
081224505277
+628129191929
+62812123929
It's unclear if you want to match numbers between 10-13 digits, or 9-11. In any case it's a simple fix (just count the initial two digits 08, or 62 as part of the total sum of digits. To implement this in php use preg_match_all:
$pat = "/^(?:\+?62|08)[0-9]{8,11}$/uim"; // note modfied range of digits
preg_match_all($pat, $str, $res);
print_r($res[0]);
Example:
http://ideone.com/GJYK7C
Result:
Array
(
[0] => 082298744807
[1] => 087884962429
[2] => 087783218768
[3] => 0818809692
[4] => 081224505277
[5] => +628129191929
[6] => +62812123929
[7] => +629490029944
)
For the life of me, I can't figure out how to write the regex to split this.
Lets say we have the sample text:
15HGH(Whatever)ASD
I would like to break it down into the following groups (numbers, letters by themselves, and parenthesis contents)
15
H
G
H
Whatever
A
S
D
It can have any combination of the above such as:
15HGH
12ABCD
ABCD(Whatever)(test)
So far, I have gotten it to break apart either the numbers/letters or just the parenthesis part broken away. For example, in this case:
<?php print_r(preg_split( "/(\(|\))/", "5(Test)(testing)")); ?>
It will give me
Array
(
[0] => 5
[1] => Test
[2] => testing
)
I am not really sure what to put in the regex to match on only numbers and individual characters when combined. Any suggestions?
I don't know if preg_match_all satisfying you:
$text = '15HGH(Whatever)ASD';
preg_match_all("/([a-z]+)(?=\))|[0-9]+|([a-z])/i", $text, $out);
echo '<pre>';
print_r($out[0]);
Array
(
[0] => 15
[1] => H
[2] => G
[3] => H
[4] => Whatever
[5] => A
[6] => S
[7] => D
)
I've got this: Example (I don't know how is written the \n) but the substitution is working.
(\d+|\w|\([^)]++\)) Not too much to explain, first tries to get a number, then a char, and if there's nothing there, tries to get a whole word between parentheses. (They can't be nested)
Check this out using preg_match_all():
$string = '15HGH(Whatever)(Whatever)ASD';
preg_match_all('/\(([^\)]+)\)|(\d+)|([a-z])/i', $string, $matches);
$results = array_merge(array_filter($matches[1]),array_filter($matches[2]),array_filter($matches[3]));
print_r($results);
\(([^\)]+)\) --> Matches everything between parenthesis
\d+ --> Numbers only
[a-z] --> Single letters only
i --> Case insensitive
Can someone help me with a regular expression to get the year and month from a text string?
Here is an example text string:
http://www.domain.com/files/images/2012/02/filename.jpg
I'd like the regex to return 2012/02.
This regex pattern would match what you need:
(?<=\/)\d{4}\/\d{2}(?=\/)
Depending on your situation and how much your strings vary - you might be able to dodge a bullet by simply using PHP's handy explode() function.
A simple demonstration - Dim the lights please...
$str = 'http://www.domain.com/files/images/2012/02/filename.jpg';
print_r( explode("/",$str) );
Returns :
Array
(
[0] => http:
[1] =>
[2] => www.domain.com
[3] => files
[4] => images
[5] => 2012 // Jack
[6] => 02 // Pot!
[7] => filename.jpg
)
The explode() function (docs here), splits a string according to a "delimiter" that you provide it. In this example I have use the / (slash) character.
So you see - you can just grab the values at 5th and 6th index to get the date values.
I have looked at several of the other regular expressions questions here and on other message boards. I am beating my head against the wall because I just can't seem to wrap my head around this. (or regular expressions in general)
I am pulling a time stamp from a MYSQL database. This is automatically generated, so it is formatted normally: 2011-12-17 21:30:56
I want to break this up into an array without having to use multiple explodes. I am assuming preg_split() is the answer here. If you have a better idea, I am all ears (though I feel like I need to figure out how to use regular expressions at some point anyway.)
In any case, I am trying to split this up at each "-" ":" and " ". After a bit of reading it seems like a character class is the answer, here is my code, that is simply not working:
$date_arr = preg_split("/ [- :] /", $order['order_date']);
This is outputting: Array ( [0] => 2011-12-17 21:30:56 )
Where am I going wrong?
The reason your preg_split fails is because of the spaces surrounding [- :].
As it's currently written in will only split on " - ", " " and " : ".
$date_arr = preg_split("/ [- :] /", ... ); // original
$date_arr = preg_split("/[- :]/", ...); // fixed
Instead of using functions such as explode and preg_split to split your string, use strtotime and getdate:
print_r (
getdate (strtotime ("2011-12-17 21:30:56"))
);
...
Array
(
[seconds] => 56
[minutes] => 30
[hours] => 21
[mday] => 17
[wday] => 6
[mon] => 12
[year] => 2011
[yday] => 350
[weekday] => Saturday
[month] => December
[0] => 1324153856
)
You have unncesary spaces in regex. Try this:
preg_split("/[- :]/", '2011-12-17 21:30:56');
Greetings All
I am trying to get the values in the 4th column from the left for this url. I can get all the values but it skips the first one (e.g. 30 i think is the value on top right now )
My regex is
~<td align="center" class="row2">.*([\d,]+).*</td>~isU
NOTE: HTML PARSING IS NOT AN OPTION RIGHT NOW AS THIS IS PART OF A HUGE SYSTEM AND CANNOT
BE CHANGED
Thanking you
Imran
You could just use:
/([\d,]+)/
As the javascript function can be exploited as a "regex selection point"
If you want your regex to work you need to use non-greedy expression, i.e. change .* to .*?
Also your first align match attribute in the HTML is surrounded in '' quotation marks, not "" in the HTML, for some weird inconsistent reason. Try this:
|<td align=["\']center["\'] class="row2">.*?([\d,]+).*?</td>|is
Edit:
$a = file_get_contents('http://www.zajilnet.com/forum/index.php?showforum=31');
preg_match_all('|<td align=["\']center["\'] class="row2">.*?([\d,]+).*?</td>|is',$a,$m);
print_r($m[1]);
Result:
Array
(
[0] => 30
[1] => 16
[2] => 56
[3] => 14
[4] => 96
[5] => 4
[6] => 0
[7] => 17
[.... and more....]