Help with regex to grab all instances of pattern from string

Hi,

I have a string of markup where I’ve got links pointing to relative locations rather than absolute locations. ie, the anchor tags have:

...href="/SomeDir"...
...href="/AnotherDir"...
...href="/YetAnotherDir"...

What I’d like to do is scan the markup and gather all the directory names into an array. I don’t need anything but the textual names. However, I also have some links that contain multiple forward-slashes, and these can be ignored. What I’m looking for in the end result is:

$array[0] = 'SomeDir';
$array[1] = 'AnotherDir';
$array[2] = 'YetAnotherDir';

I tried preg_match_all quite a few times/ways before my eclipse crashed and left me with nothing to show for it. :\ So, rather than try to trial and error my way through again, I’m hoping someone can show me the light…

BTW, the search results page is white-screening!

Thanks,

cranjled


<?php
preg_match_all('~href="/?([a-z]+)"~i', $inputString, $matches);
print_r($matches[1]);

untested

That worked fantastically!! Now I can just extract the uniques and it will work like a charm! :slight_smile: Thanks! :slight_smile:

:slight_smile:

One more freebie for you because it’s Friday night (well, here it is anyway) : [fphp]array_unique[/fphp]

Well, that was nice of you… I guess I didn’t phrase my response properly; I know array_unique well :wink: Thanks again!

LOL

You just said you wanted to extract the uniques. Who knows what scheme you were concocting to do that?
Good to know you know array_unique; it makes life easy :slight_smile:

Have a good one :slight_smile: