Jump to content

[SOLVED] ignore everything not in a sting


me1000

Recommended Posts

Sounds to me like you're trying to do regex stuff... but I don't exactly see the point. Is the string going to change? Or are you just trying to do this for that one specific example...

 

I'm sure I could help more if you were being more specific... but... well... here goes nothing.

 

<?php
$string = 'The dog [verb]jumped[/very] high';
$results = array();
preg_match_all("/\[.*?\](.*?)\[\/.*?\]/", $string, $results);
$results = $results[1];
print_r($results);
?>

 

Thats the best I can do with what what you gave me.

Looks to me like Jewbilee thinks we're using javascript... there are no "subString" or "findWord" functions.

 

The php equivalents are substr and strpos.

 

I suppose one way you could do it is...

<?php
$string = 'The dog [verb]jumped[/verb] high';
$find1 = '[verb]'; $find2 = '[/verb]';
$string = substr($string, strpos($string, $find1)+strlen($find1), strpos($string, $find2)-strpos($string, $find1)-strlen($find1));
echo $string
?>

 

That's the way to do the whole operation in one line (the finding operation that is)

You could split it up by individually finding the position of the first search and second search, then using those values in the final substr operation.

 

This way would be more like...

<?php
$string = 'The dog [verb]jumped[/verb] high';
$find1 = '[verb]'; $find2 = '[/verb]';
$find1loc = strpos($string, $find1)+strlen($find1);
$find2loc = strpos($string, $find2)-$find1loc;
$string = substr($string, $find1loc, $find2loc);
echo $string
?>

 

 

But seriously, the most efficient way to do this is using regex like this...

<?php
$string = 'The dog [verb]jumped[/verb] high';
$find = 'verb';
preg_match_all("/\[$find\](.*?)\[\/$find\]/", $string, $results); $results = $results[1];
print_r($results);
?>

 

Using that method will also return all of the values between any [verb] tags in the string, not just the first one.

 

Hope that helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.