2

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 :)

3
  • 1
    What exactly do you want to achieve? What else did you try to achieve your goal? Of cours count_chars returns an array, as given in the documentation. Have you tried dumping it to inspect the content, such that you see what it returns? Commented Oct 7, 2021 at 5:55
  • 2
    Using explode('</p>',$content) may not give you exactly what you are after, you result will still have the open tags <p>. Commented Oct 7, 2021 at 6:08
  • I suggest to work with regular expressions. Commented Oct 7, 2021 at 6:18

5 Answers 5

2

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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 />';
    }

4 Comments

Please add some explanation to your answer such that others can learn from it. What did you change, and why?
sure @NicoHaase
Thank you everyone for the comments and suggested answers here! This answer with the use of strlen and how to format the parameters is exactly what I needed. Thank you Muhammad.
@MuhammadShahid this would even be a better answer if you added some explanation to it
0

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

Comments

0

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

2 Comments

The screenshot of the PHP manual is not terribly helpful and it discriminates against users with vision impairments.
@mickmackusa I surely did not mean to discriminate anyone. Maybe opening the screenshot in a new tab is helpful. There is also a link to the page one can open if that's more helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.