tags:

views:

60

answers:

2

I have a text string that has been doctored to be web safe URLs i.e. non alphanumeric's are converted to "_".

What I want to do is search for the original name before it was converted, this means that I need some kind of wild card search i.e.

What I have

test__mytest

With wildcards

test??mytest

The string I would find with the wildcard search

test: mytest

Can someone tell me how to achieve this in PHP? Thanks

A: 

Might have found the solution here I'll update if I get it to work.

Edit: Here is my solution

$searchstring = "/".str_replace("_","(.?)", escape_string_for_regex($match))."/";

This replaces all _'s with a search for a single character in that place. I then use the following line to test $searchstring against the test string.

if (preg_match($searchstring, $matchtitle))
Chris
escape_string_for_regex should be `preg_quote($match, '/')`. (Unless you are using that inside your own function).
mcrumley
A: 

Another approach could be encoding the special characters instead of transforming them to underscores. Then you wouldn't need wildcards, you would just search for the encoded value.

For example test%3A%20page

This would also prevent the problem you would encounter if you tried convert the following two into pages:

test:page
test!page

They would both get turned into test_page, and the space/time continuum would be in danger. Probably unlikely, but still undesirable.

Syntax Error
Unfortunately I have no control over the encoding so I have to use wilcards. Thanks though
Chris