Ask a Question related to PHP Development, Design and Development.
-
Richard Grove - ®ed Eye Media #1
Regex headache
I am having a regex nightmare and can't see the wood for the trees.
I want to extract data from an HTML file. I have been using the file()
command which gets the html alright, I am just falling down with the regular
expression.
eg: <span class="something">Some Text</span><span class="something">Some
More text</span>
I want to extract the information and write it to an array. The above should
produce:
$data[0]="Some Text" , $data[1]="Some More Text"
I am such a noddy when it comes to regex, can anyone help with a code
snippet
Thanks
Regards
Richard Grove
[url]http://www.shopmaker.co.uk[/url] - Ecommerce Shop Systems
Richard Grove - ®ed Eye Media Guest
-
Bit of a headache
Hi all, I have found this bit of javascript to validate a users form input, but the problem is that it does it in steps - ie it will check the... -
Impersonation headache
I have been fighting with impersonation for quite sometime now and now matter what I have tried it just won't work. I am trying to get... -
gradient headache
Does any body see a problem with this? I get nothing. I want a triangle with blue in the lower right corner fading to transparent at the... -
Web Service headache
Hi there, I am working on a web service, which was going fine until this morning. Both it and my test client app (a simple web app) are running... -
regex causing headache ;-(
Hi there, I am having trouble with a reg ex. What it should do is, terminate if the url is /*/contact.html but it should not terminate if it... -
Jamie Isaacs #2
Re: Regex headache
Richard Grove - ®ed Eye Media wrote:
I did something like this b4. Here is some of it modified...> I am having a regex nightmare and can't see the wood for the trees.
> I want to extract data from an HTML file. I have been using the file()
> command which gets the html alright, I am just falling down with the regular
> expression.
>
> eg: <span class="something">Some Text</span><span class="something">Some
> More text</span>
> I want to extract the information and write it to an array. The above should
> produce:
> $data[0]="Some Text" , $data[1]="Some More Text"
>
> I am such a noddy when it comes to regex, can anyone help with a code
> snippet
> Thanks
>
> Regards
> Richard Grove
> [url]http://www.shopmaker.co.uk[/url] - Ecommerce Shop Systems
>
>
>
<?php
$data = array();
$quote = '<span class="something">Some Text</span><span
class="something">Some More text</span>';
// get <span ...>...</span> within $quote
preg_match_all('(<span.*?>*</span>)',$quote,$all_span, PREG_PATTERN_ORDER);
foreach($all_span[0] as $span_match)
{
echo $span_match."\n";
// $span_match = <span ...>...</span>
// get data between the span tags
preg_match_all('(>.*<)',$span_match,$all_data, PREG_PATTERN_ORDER);
foreach($all_data[0] as $data_match)
{
echo ' '.substr($data_match,1,strlen($data_match)-2)."\n";
array_push($data,substr($data_match,1,strlen($data _match)-2));
}
}
print_r($data);
?>
now $data should have all the stuff you need.
-JI
Jamie Isaacs Guest
-
Richard Grove - ®ed Eye Media #3
Re: Regex headache
"Jamie Isaacs" <jamie@shsu.edu> wrote in message
news:cjeeai$dgm@library1.airnews.net...regular> Richard Grove - ®ed Eye Media wrote:> > I am having a regex nightmare and can't see the wood for the trees.
> > I want to extract data from an HTML file. I have been using the file()
> > command which gets the html alright, I am just falling down with theshould> > expression.
> >
> > eg: <span class="something">Some Text</span><span class="something">Some
> > More text</span>
> > I want to extract the information and write it to an array. The abovePREG_PATTERN_ORDER);>> > produce:
> > $data[0]="Some Text" , $data[1]="Some More Text"
> >
> > I am such a noddy when it comes to regex, can anyone help with a code
> > snippet
> > Thanks
> >
> > Regards
> > Richard Grove
> > [url]http://www.shopmaker.co.uk[/url] - Ecommerce Shop Systems
> >
> >
> >
> I did something like this b4. Here is some of it modified...
>
> <?php
> $data = array();
> $quote = '<span class="something">Some Text</span><span
> class="something">Some More text</span>';
>
> // get <span ...>...</span> within $quote
> preg_match_all('(<span.*?>*</span>)',$quote,$all_span,> foreach($all_span[0] as $span_match)
> {
> echo $span_match."\n";
> // $span_match = <span ...>...</span>
>
> // get data between the span tags
> preg_match_all('(>.*<)',$span_match,$all_data, PREG_PATTERN_ORDER);
> foreach($all_data[0] as $data_match)
> {
> echo ' '.substr($data_match,1,strlen($data_match)-2)."\n";
> array_push($data,substr($data_match,1,strlen($data _match)-2));
> }
> }
>
> print_r($data);
> ?>
>
> now $data should have all the stuff you need.
> -JI
Many thanks, we are on the right road now.
I changed it to this but it doesn't work.
preg_match_all('(<span class="bodybold">*</span>)',$lines[$a],$all_span,
PREG_PATTERN_ORDER);
I would like to get data from between <span class="bodybold">data</span>
Any ideas?
Richard Grove - ®ed Eye Media Guest
-
Jamie Isaacs #4
Re: Regex headache
to match just the ones with bodybold in the span tag try this:
<?php
$data = array();
$quote = '<span class="bodybold">Some Text</span><span
class="something">Some More text</span>';
// get <span ...>...</span> within $quote
preg_match_all('(<span.*?(bodybold).*?>.*?</span>)',$quote,$all_span,
PREG_PATTERN_ORDER);
foreach($all_span[0] as $span_match)
{
echo $span_match."\n";
// $span_match = <span ...>...</span>
// get data between the span tags
preg_match_all('(>.*<)',$span_match,$all_data, PREG_PATTERN_ORDER);
foreach($all_data[0] as $data_match)
{
echo ' '.substr($data_match,1,strlen($data_match)-2)."\n";
array_push($data,substr($data_match,1,strlen($data _match)-2));
}
}
print_r($data);
?>
Jamie Isaacs Guest
-
Richard Grove - ®ed Eye Media #5
Re: Regex headache
"Jamie Isaacs" <jamie@shsu.edu> wrote in message
news:cjer6c$nbc@library1.airnews.net...> to match just the ones with bodybold in the span tag try this:
>
> <?php
> $data = array();
> $quote = '<span class="bodybold">Some Text</span><span
> class="something">Some More text</span>';
>
> // get <span ...>...</span> within $quote
> preg_match_all('(<span.*?(bodybold).*?>.*?</span>)',$quote,$all_span,
> PREG_PATTERN_ORDER);
> foreach($all_span[0] as $span_match)
> {
> echo $span_match."\n";
> // $span_match = <span ...>...</span>
> // get data between the span tags
> preg_match_all('(>.*<)',$span_match,$all_data, PREG_PATTERN_ORDER);
> foreach($all_data[0] as $data_match)
> {
> echo ' '.substr($data_match,1,strlen($data_match)-2)."\n";
> array_push($data,substr($data_match,1,strlen($data _match)-2));
> }
> }
>
> print_r($data);
> ?>
Many thanks Jamie,
I'll give it a spin
Regards
Richard Grove
[url]http://www.shopmaker.co.uk[/url] - Ecommerce Shop Systems
Richard Grove - ®ed Eye Media Guest



Reply With Quote

