Urgent Yet Easy Help Needed With Preg OFFSET CAPTURE

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Urgent Yet Easy Help Needed With Preg OFFSET CAPTURE

Post by kaisellgren »

Hi,

I have a search page. Users can type like abnorm[al][al] and it matches both abnormal and abnormla text (regex). The problem is that how do I know how long is the search word? If I use strlen($search) it says 14 characters (abnorm[al][al]), but I need it to be 8 (abnormal) how would I do this? I am using OFFSET CAPTURE in preg match sentence can I somehow include it there?


Search for: te[sx]t
Results: text, test
Length: 7 (SHOULD BE 4!! :( )
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

if ($start = strpos($search, '[') && ($end = strpos($search, ']')))
{
    $pos = $end - $start;
    $strlen = strlen($search) - $pos;
    echo $strlen;
}
Untested
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

Well yea it works or something like that, but anyone can type also {}, +, ? and other regex metacharacters so that's not going to help me a lot :(
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

I got it :)

I used

Code: Select all

foreach ($matches[0] as $array => positions)
echo strlen($positions[0])
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

What happens if someone searches for "a.+"
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

scottayy wrote:What happens if someone searches for "a.+"
It says 2, 3, ... depending on the length.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

If it's a regex match, shouldn't it match all words starting with a? :P Just throwing out hypothetical situations here. Glad you got it to work, and sorry I wasn't able to help!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

scottayy wrote:If it's a regex match, shouldn't it match all words starting with a? :P Just throwing out hypothetical situations here. Glad you got it to work, and sorry I wasn't able to help!
Yes but I am replacing + * and ? characters with \ before inserting in MySQL REGEXP clause. I am accepting {} but with limits.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Very nice.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply