Jump to content

Very simple Regex help


cordoprod

Recommended Posts

Hey,

I'm actually starting to understand some simple regex.

But there is one thing i'm struggling with. I know how to parse simple tags and stuff.

 

But this is the code i need to parse:

var bmenuItems =
[
["Halden", "Tab01", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_n_icon.gif", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_s_icon.gif", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_s_icon.gif", "Oversikt for region 01 Halden", "0"],
["Sarpsborg", "Tab02", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_n_icon.gif", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_s_icon.gif", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_s_icon.gif", "Oversikt for region 02 Sarpsborg", "0"],
["Fredrikstad", "Tab03", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_n_icon.gif", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_s_icon.gif", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_s_icon.gif", "Oversikt for region 03 Fredrikstad", "0"],
["Moss", "Tab04", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_n_icon.gif", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_s_icon.gif", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_s_icon.gif", "Oversikt for region 04 Moss", "0"],
["Indre Østfold", "Tab05", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_n_icon.gif", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_s_icon.gif", "http://hafas.websrv05.reiseinfo.no/hafas-res/dev/nri/img/vs_rutebok/style02_2_s_icon.gif", "Oversikt for region 05 Indre Østfold", "0"],
];

 

I need to get out all the ones with inside ["xxxxx", 

For example: Halden, Sarpsborg

 

I've tried this:

$linjer = preg_match_all('/([")(\w.*)(",.*)/', $htmlCode, $linjerMatches);

 

But when i do var_dump the output is NULL.

Link to comment
Share on other sites

Your regular expression is malformed, it will thrown an E_WARNING (which your error reporting should be displaying!) stating as much.

 

One way to access those values with a regular expression is:

$places = array();
if (preg_match_all('/^\["([\pL ]+)",/mu', $subject, $matches)) {
$places = $matches[1];
}

print_r($places);

 

The expression looks for "the start of a line, followed by the characters [", followed by one or more unicode letters or ASCII space characters (these are captured for use later), followed by the characters ",. The pattern modifiers mu ask the matching engine to use multiline mode (so ^ matches at the start of any line rather than just the start of the entire string) and UTF-8 mode (so non-ASCII characters can be matched easily).

 

Link to comment
Share on other sites

Thanks for the reply but unfortunatly it didn't work out.

I outputs an array like this:

array(2) {
  [0]=>
  array(0) {
  }
  [1]=>
  array(0) {
  }
}

 

But be aware that the HTML i posted, was not the whole parse html.

It was a big website, so the first line in the HTML code wasn't the first line in the web page code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.