me1000 Posted July 13, 2007 Share Posted July 13, 2007 Does anyone know of a function to ignore everything that is not within 2 defined strings? for example, 'The dog [verb]jumped[/very] high' but only display the word 'jumped' thanks Quote Link to comment Share on other sites More sharing options...
Jewbilee Posted July 13, 2007 Share Posted July 13, 2007 write your own custom function to use the substring() function to filter out words within a given tag. findWord(tag);. And you would have it look for the start tag, the end tag, and return the stuff in the middle. Quote Link to comment Share on other sites More sharing options...
lococobra Posted July 13, 2007 Share Posted July 13, 2007 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. Quote Link to comment Share on other sites More sharing options...
me1000 Posted July 13, 2007 Author Share Posted July 13, 2007 oops sorry, I forgot to reread my post, $string = 'The dog [verb]jumped[/verb] very high'; but then only display the word 'jumped' Quote Link to comment Share on other sites More sharing options...
lococobra Posted July 13, 2007 Share Posted July 13, 2007 The code I created should do just that. However, there are much more simplistic ways of doing it if you only want it to work for that specific case... Quote Link to comment Share on other sites More sharing options...
me1000 Posted July 13, 2007 Author Share Posted July 13, 2007 at the moment just this one tag is all i am interested in, and I am all for simple, Quote Link to comment Share on other sites More sharing options...
lococobra Posted July 13, 2007 Share Posted July 13, 2007 Then how about... <?php echo substr('The dog [verb]jumped[/very] high',14,6); ?> Quote Link to comment Share on other sites More sharing options...
me1000 Posted July 13, 2007 Author Share Posted July 13, 2007 not exactly what I want, because the text will vary, I thought you were asking if i was only interested the [verb] [/verb] tags, so which way would be best? your way or Jewbilee's way? Quote Link to comment Share on other sites More sharing options...
lococobra Posted July 13, 2007 Share Posted July 13, 2007 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. Quote Link to comment Share on other sites More sharing options...
me1000 Posted July 13, 2007 Author Share Posted July 13, 2007 alright, thanks! you just made my life a lot easier! Thanks again! Quote Link to comment Share on other sites More sharing options...
lococobra Posted July 13, 2007 Share Posted July 13, 2007 I edited my post because I realized that regex is much more efficient, note the last part. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.