Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/136603-strpos-and-apostrophies-single-quotes/
Share on other sites

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.