0

I need to remove "from products where id = 153183". The id can change and so I need to use a preg_replace to remove everything after the word "from" and then possibly use str_replace to remove from. I have the below but this will only remove the word from if its last in a string. Can anyone suggest what I need to add?

//doesn't work

$str = "select id, productdescription, category, price, code, manufacturer, categoryid from  products where id = 153183";

$new_str = preg_replace('/from$/', '', $str);
3
  • 2
    Are you sure you need to remove FROM clause, not WHERE only? Commented May 17, 2013 at 14:48
  • There's not need for a regular expression. Especially when matching on a simple string. Commented May 17, 2013 at 14:53
  • yes I'm sure what I want to remove from he string. I don't know what he ID will be in the string therefore how can I do it without a reg ex? Commented May 17, 2013 at 14:55

6 Answers 6

2

you can do this:

$new_str = stristr($str, " from ", true);

As from is a reserved word in SQL you can't find this word elsewhere without quote or backtick (so I add a space after and before).

It returns the string before the "from" word.

strstr for a case-sensitive search.

UPDATE: Regular expression (not really needed for this question) :

$str = 'select id, productdescription, category, price, code, manufacturer, categoryid from  products where id = 153183';

preg_match('/(.*)\sfrom\s.*$/i', $str, $matches); // i for the insensitive case search

$new_str = $matches[1]; // Contains what you want
Sign up to request clarification or add additional context in comments.

6 Comments

+ for a non-regular expression solution.
Wouldn't stristr be a better choice?
Now we just need to consider the possibility of, say, newline characters around FROM instead of plain spaces.
Use regex for more complex search operation.
@Blazemonger: or of select foo_from from bar... :-)
|
1

You probably want something like /from.*$/ or simply /from.*/

5 Comments

If the word from is present multiple times you will be in trouble though.
What about a statement like "SELECT roads_from_rome FROM maps"? \bfrom\b or \sfrom\s should help with that.
@Blazemonger: Yeyeye, but OP isn't going to solve that one using a regexp. He'd need a proper query parser. :-)
.*? is the not greedy version
Not sure what you mean by greedy but the first one works thanks. 'from' will only be in there the once. BTW - Im a she not a he @Denis ;)
1

I'm a little confused by your question, this should get you going though.

<?php
$sql = 'SELECT * FROM products WHERE id = 153183';
$sql = preg_replace('~from products where id = [0-9]+$~i', '', $sql);
echo $sql;
/*
    SELECT * 
*/

Comments

0

You could use a simple str_replace. Not sure how you are getting the id since it can change, I am assuming you have a variable for it.

$str = "select id, productdescription, category, price, code, manufacturer, categoryid from  products where id = 153183";
$new_str = str_replace("from products where id = " . $id, "", $str)

1 Comment

The value of id can change.
0
$string = "select id, productdescription, category, price, code, manufacturer, categoryid from  products where id = 153183";
$part1 = substr("$string",0, strrpos($string,'from  products where id '));
var_dump($part1);

Since the bulk of the string is static you can use a sub string up to the offending section.

RESULTS:

string(79) "select id, productdescription, category, price, code, manufacturer, categoryid "

Comments

0

Combination of preg_replace and str_replace will also work:

<?php

$str = "select id, productdescription, category, price, code, manufacturer, categoryid from  products where id = 153183";
$id_removed =  preg_replace('/\d/', '', $str); //remove dynamic product id
$new_str = str_replace("from  products where id =",'', $id_removed);
echo $new_str;


?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.