Page 1 of 1
Problem with string
Posted: Thu Oct 20, 2005 12:00 pm
by freddylocks
Hi everyone
I have a string... it looks like this....
Note that there is a space on the end of the string.
However i am having problems parsing this string and getting the output 14/10/2005
Does anyone have any suggestions on how to get this output from this string?
Does the strange character at the beginning make any difference and do i need to strip that character out first?
Many Thanks
Freddy
Posted: Thu Oct 20, 2005 1:12 pm
by foobar
Use a regex:
Code: Select all
$string = '" WEEK ENDING: 14/10/2005" ';
$extract = preg_replace('#[\d]{2}/[\d]{2}/[\d]{4}#', "\\1.\\2.\\3", $string);
$date = strtotime($extract);
Still Problems
Posted: Fri Oct 21, 2005 2:44 am
by freddylocks
Many Thanks for your reply....
I am still having a issue...
The $string line is delivered via a multiline array and i use a for statement to get each line from the array:
Code: Select all
for($line=0;$line<count($array);$line++){
$string=$array[$line];
$extract = preg_replace('#[\d]{2}/[\d]{2}/[\d]{4}#', "\\1.\\2.\\3", $string);
$rdate = strtotime($extract);
}
echo $rdate;
However it does not seem to work, I just get a output from $rdate of -1.
As the above is processing multiple lines, is it likely that it is not processing on the correct line?
Is there a way of checking to see if the line contains a date?
Thanks
Freddy
Posted: Fri Oct 21, 2005 3:48 am
by shiznatix
Code: Select all
for($line=0;$line<count($array);$line++){
$string=$array[$line];
preg_match('#[\d]{2}/[\d]{2}/[\d]{4}#', $match, $string);
if (!empty($match))
$rdate[] = strtotime($match);
}
print_r($rdate);
but I am not sure if that regex would work. im not good at regex but it looks a little funky to me.
Nope
Posted: Fri Oct 21, 2005 4:13 am
by freddylocks
I have tried adding that into the script.
The issue i think i am having with the regex... is that it is not picking out the date out of the string. I am not sure why but it just doesn't seem to be doing it.
I have been looking at the raw string code and the output code...
the output after echoing using HtmlSpecialChars shows as this....
I don't know if this makes any difference to using preg_match or preg_replace.
However even without a if statement it still only shows as -1 for all lines of the array... even the line which has the above line. I moved the echo of $rdate to within the for statement.
Cheers
Freddy
Posted: Fri Oct 21, 2005 5:27 am
by n00b Saibot
shiznatix wrote:Code: Select all
preg_match('#[\d]{2}/[\d]{2}/[\d]{4}#', $match, $string);
you messed the parameters, fella... should be
Code: Select all
preg_match('#\d{2}/\d{2}/\d{4}#', $string, $match);
Posted: Fri Oct 21, 2005 5:39 am
by shiznatix
aye i don't do regex. i should really learn how to use that stuff though

Still no joy
Posted: Fri Oct 21, 2005 6:13 am
by freddylocks
Well i changed it as you mentioned and i now have...
Code: Select all
for($line=0;$line<count($array);$line++){
$string=$array[$line];
preg_match('#[\d]{2}/[\d]{2}/[\d]{4}#', $string, $match);
if (!empty($match))
$rdate[] = strtotime($match);
}
print_r($rdate);
Which gives a result of:
Array ( [0] => -1 )
I have also noticed that by running the following peice of code, the preg_match does actually find the right line, however it always comes back with -1.
Code: Select all
for($line=0;$line<count($array);$line++){
echo "<PRE>",HtmlSpecialChars($array[$line]),"</PRE>\n";
$string=$array[$line];
preg_match('#[\d]{2}/[\d]{2}/[\d]{4}#', $string, $match);
if (!empty($match))
$rdate[] = strtotime($match);
}
print_r($rdate);
So now the only issue i am having is that it finds the correct line, however it doesn't display the capture text.
I am not sure what i am doing wrong here.
Cheers
Freddy
Posted: Fri Oct 21, 2005 9:14 am
by foobar
n00b Saibot wrote:shiznatix wrote:Code: Select all
preg_match('#[\d]{2}/[\d]{2}/[\d]{4}#', $match, $string);
you messed the parameters, fella... should be
Code: Select all
preg_match('#\d{2}/\d{2}/\d{4}#', $string, $match);
Yep, I did

. Should've mentioned it wasn't tested....
Also, if it's multiline, try this:
Code: Select all
preg_match('#\d{2}/\d{2}/\d{4}#m', $string, $match);
Still not giving result
Posted: Fri Oct 21, 2005 9:22 am
by freddylocks
Ok well i tried that however it is still giving the result as -1.
As the $string is based on $array[$line] , the $string is a single line.
The issue seems to be getting the string that it is looking at, back out into a variable.
Cheers
Freddy
Posted: Fri Oct 21, 2005 9:38 am
by foobar
Do a search for Regex Coach on Google.
Posted: Fri Oct 21, 2005 3:42 pm
by chrys
Umm, I would just do:
Code: Select all
$string = "what you had up there";
$tmp = split( ":", $string );
$date = trim($tmp[1]);
Then $date would contain the date.
Posted: Sat Oct 22, 2005 1:07 am
by n00b Saibot
Sorry for posting late... Guys you are missing another thing .... $string is an array()

Posted: Sat Oct 22, 2005 4:49 am
by foobar
n00b Saibot wrote:Sorry for posting late... Guys you are missing another thing .... $string is an array()

Uh...no it isn't.
Posted: Sat Oct 22, 2005 5:38 am
by n00b Saibot

omg, i dunno whatever made me write $string, i meant $match
