lilleyp Posted December 12, 2008 Share Posted December 12, 2008 Hi, I'm having trouble finding single quotes in a string using strpos, strpos is returning the incorrect position. The code parses SQL statements and extracts any parameters (e.g. where field = 'Parameter'). The SQL parameters are encapsulated in apostrophies (or single quotes). The code looks like this: $SingleQuote = "'"; //have tried escaping this but the results are teh same $StartPos = strpos($String,$SingleQuote,$ctr); if($StartPos === false) { //no further processing required } else { $EndPos = strpos($String,$SingleQuote,$StartPos + 1); } kl($String . " -> " . $SingleQuote . ' -> ' . $StartPos . ' -> ' . $EndPos); //this is my own function that basically exits the application. ....returns this result select cat_table, cat_key, cat_field_status, cat_field_location from bp_object_type where object_type = 'AP' -> ' -> 135 -> 138 The first apostrophe is at position 105, not 135 and the second at 108 not 138. I've tested the above code by using other characters and it works perfectly. I've also tried using addslashes and the result is the same. I really want to use the strpos function as it is (I'm lead to believe) the fastest of all the string searching functions and there will be a large number of calls to this function at any given time. Any help is greatly appreciated. Thanks in advance We are using v4.3.9 here at work Quote Link to comment https://forums.phpfreaks.com/topic/136603-strpos-and-apostrophies-single-quotes/ Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 kl($String . " -> " . $SingleQuote . " -> " . $StartPos . " -> " . $EndPos); Quote Link to comment https://forums.phpfreaks.com/topic/136603-strpos-and-apostrophies-single-quotes/#findComment-713267 Share on other sites More sharing options...
haku Posted December 12, 2008 Share Posted December 12, 2008 You didn't show what the value of $String is, so we can't really help you. Quote Link to comment https://forums.phpfreaks.com/topic/136603-strpos-and-apostrophies-single-quotes/#findComment-713271 Share on other sites More sharing options...
lilleyp Posted December 12, 2008 Author Share Posted December 12, 2008 Apologies, It is actually there, but not obvious, the value of $String is: select cat_table, cat_key, cat_field_status, cat_field_location from bp_object_type where object_type = 'AP' Quote Link to comment https://forums.phpfreaks.com/topic/136603-strpos-and-apostrophies-single-quotes/#findComment-713303 Share on other sites More sharing options...
DarkWater Posted December 12, 2008 Share Posted December 12, 2008 Why not use a regex? <?php $string = "SELECT cat_table, cat_key, cat_field_status, cat_field_location FROM bp_object_type WHERE object_type = 'AP'"; preg_match_all('/(\w+)\s*=\s*([\'"])(\w+)\2/', $string, $matches); print_r($matches); $matches[1] and $matches[3] will be the part before the equals sign and after it, respectively. Quote Link to comment https://forums.phpfreaks.com/topic/136603-strpos-and-apostrophies-single-quotes/#findComment-713304 Share on other sites More sharing options...
lilleyp Posted December 12, 2008 Author Share Posted December 12, 2008 Regex functions are going to be too expensive for what I'm trying to do (supposed to be significantly slower than strpos() See:http://lzone.de/articles/php-string-search.htm ). I'd much prefer a solution to strpos.... Quote Link to comment https://forums.phpfreaks.com/topic/136603-strpos-and-apostrophies-single-quotes/#findComment-713351 Share on other sites More sharing options...
DarkWater Posted December 12, 2008 Share Posted December 12, 2008 1) You're making it much harder than it really is by not using a regular expression here. Honestly. 2) They are not THAT slow, especially knowing that you're going to have to do several strpos() calls and stuff. 3) My solution grabs everything in one go and is very simple... Good luck doing it with strpos() if you're still going to try it. I wouldn't. Quote Link to comment https://forums.phpfreaks.com/topic/136603-strpos-and-apostrophies-single-quotes/#findComment-713359 Share on other sites More sharing options...
haku Posted December 12, 2008 Share Posted December 12, 2008 Let's see your kl function. Quote Link to comment https://forums.phpfreaks.com/topic/136603-strpos-and-apostrophies-single-quotes/#findComment-713400 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.