Jump to content

If spaces occurs 4 times or more within exploded quotes...


ryancooper

Recommended Posts

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;

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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"

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.