Regex issue in PHP: DB vs String

I am having the darkest time figuring this out. I have function that that takes a string ( that is read from a DB then stored in a variable) and looks for a pattern match using a regex . pretty basic stuff. except that the regex always returns false.

In the process of testing I found that the expression works perfectly tested on a string that that is ‘hard coded’ within the script , as opposed to string brought in via the DB.

Whats weirder still is if I OUPUT the string to HTML, copy that output in to a variable in the script , and run that variable through the script it returns all matches as expected!

so it seems to me that the regex works ( when tested by evaluating a hardcoded test string)
And the patterns it looks for are within the data returned from the DB. ( as tested by outputting the DB data to HTML, and copy pasting that data from the View Source in to a variable and testing the regex with it). I even went to the mySQL control panel and copied the string directly from there and the regex once again performed as expected,

the only thing I can the raw string from the DB could be inserting invisible characters that are not rendered or outputted when echoed to HTML.

I would be much obliged if anyone could suggest how to test for these invisible characters or any suggestion as to why a regex would not work on a ‘raw’ string from a DB but apparently works perfectly on the SAME string when copy-pasted as part of the script

I guess you could try count_chars to see if there are any “invisible” characters i.e. the count is not what is expected.

Then iterate through the string with ord() (unless if UTF-8?)

BRILLIANT ! It would not have occurred to me to do that.

Postng because in case someone else comes across a similar problem.

While Mittineague’s answer was in fact brilliant, I discovered that there wa sin fact NO difference between the sting from my DB and the one copied pasted from the control panel. :confused:

however the process did make me look mat my test script again. in my test scrip where i use SINGLE quotes to enclose my pattern , in my production script i had used DOUBLE quotes.

I will admit I am still uncertain why this would make all the difference, but it did. once I changed my double quotes to single quotes everything rand as expected!

It must depend on the pattern. Stuff inside of double quotes gets interpreted so there might be a difference between '\n' and "\n" but if this is so I’m very surprised I never got bit by it before.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.