Chris
Chris

Reputation: 351

PHP - how can I get the number of characters in an array value?

I'm trying to figure out how to get a character count from a group of values in an array, so I can test it in an if statement.

Best if I give you an example of what I want to do:


//sample

$content = '<p>this is p 1</p><p>this is p 2</p><p>this is p 3</p><p>this is p 4</p><p>$test</p>';
$parts = explode('</p>',$content);

foreach ($parts as $key => $value) {
  $test = count_chars($value[0] + $value[1] + $value[2]);

  if ($test > 40) {
    echo 'Yep, there are '.$test.' characters in the first three paragraphs<br />';
  }
  else { 
    echo 'Nope. There are '.$test.' characters in the first three paragraphs<br />'; 
  }
}

I don't work with arrays much, so not sure how to do this. Right now $test just gives me 'array'.

And I know this is going to repeat the same string, this is not the final product, stripped out as many details as I could so we could focus on the situation I'm stuck on :)

Upvotes: 2

Views: 1145

Answers (5)

Lajos Arpad
Lajos Arpad

Reputation: 76508

count_chars have $mode as 0 by default. See:

enter image description here

You can call it with different mode to get a string as a result. But based on what you described, this is not what you want. You will need to call strlen instead:

$totalLength = $myStringArray[0];
for ($index = 1; $index < count($myStringArray); $index++) {
    $totalLength += $myStringArray[$index];
}

as about the explode, you are removing the </p> from the string, which I suppose is your actual goal. But you still left <p> inside the string. You will need to remove it too if you want to exclude it. You can use str_replace for that purpose, like:

$length = str_replace('<p>', '', str_replace('</p>', '', $content));

Upvotes: 0

Nigel Ren
Nigel Ren

Reputation: 57121

Assuming you don't mind the extra <p> parts of the string being counted, you could simplify this to using implode() without a seperator as it will just put all of the parts back together and then take the strlen() of the result...

$parts = explode('</p>',$content);

$test = strlen(implode($parts));

Using count_chars() isn't correct as it Counts the number of occurrences of every byte-value (0..255) in string and returns it in various ways..

Also using + is adding numeric values and not strings.

Upvotes: 1

miile7
miile7

Reputation: 2393

I suggest to use array_reduce. This handles a variable number of values in your $content.

array_reduce takes each value of the array and performs the callback on it. The returned value is then somehow mixed together with the results of the previous content.

In the following code array_reduce takes the initial value 0 as $carray. Then it takes the first element from the $content array (as $text) and adds the length of the $text to the $carray. The result is the new $carray for the next array value. This generates the full length.

$content = '<p>this is p 1</p><p>this is p 2</p><p>this is p 3</p><p>this is p 4</p><p>$test</p>';

$parts = explode('</p>', $content);

$test = array_reduce($parts, function($carry, $text){
    return strlen($text) + $carry; 
}, 0);

Note that your $content still has the <p> tags which are counted. There are a few ways to count only the text without the tags. The easiest but also least flexible way is to use strip_tags. This will count your text but all tags are removed.

$content = '<p>this is p 1</p><p>this is p 2</p><p>this is p 3</p><p>this is p 4</p><p>$test</p>';

$test = strlen(strip_tags($content));

Another possibility is to remove the <p> tags by e.g. replacing them. I would prefere a regular expression but in exact this case str_replace is enough:

$content = '<p>this is p 1</p><p>this is p 2</p><p>this is p 3</p><p>this is p 4</p><p>$test</p>';

$parts = explode('</p>', $content);

$test = array_reduce($parts, function($carry, $text){
    return strlen(str_replace("<p>", "", $text)) + $carry; 
}, 0);

Depending on what you need you can also parse your html. This is a lot more powerful but may also be too much:

$content = '<p>this is p 1</p><p>this is p 2</p><p>this is p 3</p><p>this is p 4</p><p>$test</p>';

$dom = new DOMDocument();
$dom->loadHTML("<html><body>".$content."</body></html>");

$test = strlen($dom->textContent);

Upvotes: 0

Shahid Rasheed
Shahid Rasheed

Reputation: 391

Try this solution:

if you want to check only first 3 paragraphs then you don't need any for each loop.

<?php

$content = '<p>this is p 1</p><p>this is p 2</p><p>this is p 3</p><p>this is p 4</p><p>$test</p>';

$parts = explode('</p>', $content);

$test = strlen($parts[0].$parts[1].$parts[2]);

    if ($test > 40) {
        echo 'Yep, there are ' . $test . ' characters in the first three paragraphs<br />';
    } 
    else {
        echo 'Nope. There are ' . $test . ' characters in the first three paragraphs<br />';
    }

Upvotes: 0

adam
adam

Reputation: 367

If you want to count all character use strlen() insted of count_chars() like this

$test = strlen($value[0].$value[1].$value[2]);

count_chars() function will returns information about characters used in a string (for example, how many times an ASCII character occurs in a string, or which characters that have been used or not been used in a string) count_chars

and you don't need use foreach() since explode() will give you one dimension array, simply what you need is just like this

$value = explode('</p>',$content);

Upvotes: 2

Related Questions