ryancooper Posted May 9, 2011 Share Posted May 9, 2011 I keep having trouble with exploded arrays, every function i go to use within the exploded quotes requires a string not an array. Below im trying to count the spaces and if there are four or more change the data string to the halt string. The problem is most of the functions require a string, is there another way to only count the space characters within quotes without exploding the quotes? $data = 'Hello World This is a test string!'; $halt = 'String had more than 4 spaces.'; $arr = explode('"', $data); if (substr_count($arr, ' ') >= 4) { $data = implode('"', $arr); $data = $halt; Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 9, 2011 Share Posted May 9, 2011 I don't get your code, you are exploding your string ($data) on a quote. But your string doesn't contain any quotes. What are trying to do only allow four words to be in a string. Quote Link to comment Share on other sites More sharing options...
ryancooper Posted May 9, 2011 Author Share Posted May 9, 2011 I don't get your code, you are exploding your string ($data) on a quote. But your string doesn't contain any quotes. What are trying to do only allow four words to be in a string. Sorry, updated code below. Im trying only allow 4 words or spaces within quotes... i can do it for a simple string, but not only within quotes. $data = 'Hello World "This is a test string! Jack and Jill went up the hill."'; $halt = 'String had more than 4 spaces.'; $arr = explode('"', $data); if (substr_count($arr, ' ') >= 4) { $data = implode('"', $arr); $data = $halt; Quote Link to comment Share on other sites More sharing options...
spiderwell Posted May 9, 2011 Share Posted May 9, 2011 you probably need a regular expression of somesort Quote Link to comment Share on other sites More sharing options...
Zane Posted May 9, 2011 Share Posted May 9, 2011 This seems to me what you are trying to accomplish $data = 'Hello World This is a test string!'; $halt = 'String had more than 4 spaces.'; $arr = explode(' ', $data); if (count($arr) >= 4) { $data = $halt; else { $data = implode(' ', $arr); } Quote Link to comment Share on other sites More sharing options...
ryancooper Posted May 10, 2011 Author Share Posted May 10, 2011 This seems to me what you are trying to accomplish $data = 'Hello World This is a test string!'; $halt = 'String had more than 4 spaces.'; $arr = explode(' ', $data); if (count($arr) >= 4) { $data = $halt; else { $data = implode(' ', $arr); } Pretty close, except i need it to only look inside exploded quotes... so: Hello World "This is a test string! Jack and Jill went up the hill." would change the string to the halt. Hello World "This is a test!" would not change because there is only three spaces within the quotes. Quote Link to comment Share on other sites More sharing options...
Zane Posted May 10, 2011 Share Posted May 10, 2011 I don't get your code, you are exploding your string ($data) on a quote. But your string doesn't contain any quotes. Quote Link to comment Share on other sites More sharing options...
ryancooper Posted May 10, 2011 Author Share Posted May 10, 2011 I don't get your code, you are exploding your string ($data) on a quote. But your string doesn't contain any quotes. I updated the code in my second post as im unable to edit the original. I don't get your code, you are exploding your string ($data) on a quote. But your string doesn't contain any quotes. What are trying to do only allow four words to be in a string. Sorry, updated code below. Im trying only allow 4 words or spaces within quotes... i can do it for a simple string, but not only within quotes. $data = 'Hello World "This is a test string! Jack and Jill went up the hill."'; $halt = 'String had more than 4 spaces.'; $arr = explode('"', $data); if (substr_count($arr, ' ') >= 4) { $data = implode('"', $arr); $data = $halt; Quote Link to comment Share on other sites More sharing options...
mikosiko Posted May 10, 2011 Share Posted May 10, 2011 one option: (assuming that you can have only 1 "substring" into your 'string') $data = 'Hello World "This is a test string! Jack and Jill went up the hill."'; preg_match_all('/\".*\"/', $data, $matches); echo "<br /> There are " . substr_count($matches[0][0], ' ') . " spaces"; you should incorporate code to validate in case your 'string' doesn't include a "substring" Quote Link to comment Share on other sites More sharing options...
ryancooper Posted May 10, 2011 Author Share Posted May 10, 2011 Awesome thanks! 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.