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;

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;

 

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

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.

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;

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"

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.