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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.