Page 1 of 1
substr_replace remove characters before "
Posted: Tue Nov 02, 2010 10:50 pm
by tutigan
I'm outputting a large amount of info, that goes roughly like this:
uselessinfohere "important stuff"
from the following code:
Code: Select all
$cmd = "net rpc service list -I 192.168.1.1 -U Administrator%Password";
exec($cmd, $output);
foreach ($output as $line) {
echo $line . "<br />";
}
what I want to be able to do is to remove everything BEFORE that first quotation mark...
I know how to remove the last quotation mark:
$line = substr_replace($line, "", -1);
but am unsure how to do up to the first, as each $line has a different amount of characters before the "
any help is greatly appreciated ^_^
~Tutigan
Re: substr_replace remove characters before "
Posted: Tue Nov 02, 2010 11:53 pm
by s.dot
Assuming each line will always be in that format and the last character of the string is always " and there will never be a " in between the opening " and closing ".. this would work
Code: Select all
$string = 'here is some useless junk "and this is really important"';
//capture in part in quotes, and remove the quotes
$ret = str_replace('"', '', substr($string, strpos($string, '"')));
echo $ret;
Re: substr_replace remove characters before "
Posted: Wed Nov 03, 2010 2:20 am
by tutigan
that works fine!
except... I made a noob mistake!
the bit BEFORE the quotation marks is the important part, the parts between the quotation marks is useless

Re: substr_replace remove characters before "
Posted: Wed Nov 03, 2010 2:26 am
by s.dot
lol, that makes it a little easier
Code: Select all
$string = 'here is some important stuff "and this is junk"';
echo substr($string, 0, strpos($string, '"'));
String manipulation is fun.
Re: substr_replace remove characters before "
Posted: Wed Nov 03, 2010 8:31 am
by twinedev
s.dot wrote:Assuming each line will always be in that format and the last character of the string is always " and there will never be a " in between the opening " and closing ".. this would work
Code: Select all
$string = 'here is some useless junk "and this is really important"';
//capture in part in quotes, and remove the quotes
$ret = str_replace('"', '', substr($string, strpos($string, '"')));
echo $ret;
I know this isn't relevant now that the need changed, but just wanted to let you know this could be done without the str_replace. I'm always on the lookout to reduce unnecessary calls.
Code: Select all
$ret = substr($string,strpos($string,'"')+1,-1)
-Greg