substr_replace remove characters before "

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tutigan
Forum Newbie
Posts: 18
Joined: Thu Oct 28, 2010 5:43 am

substr_replace remove characters before "

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: substr_replace remove characters before "

Post 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;
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
tutigan
Forum Newbie
Posts: 18
Joined: Thu Oct 28, 2010 5:43 am

Re: substr_replace remove characters before "

Post 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 :banghead:
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: substr_replace remove characters before "

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: substr_replace remove characters before "

Post 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
Post Reply