Home » Php » jquery – How to break div id text after n number of characters in PHP

jquery – How to break div id text after n number of characters in PHP

Posted by: admin February 25, 2020 Leave a comment

Questions:

I trying to break long text.

The text look like this: Inquire about space/Enter Library. The text is currently separated using the slash. So we can use the / to tell the code where to break though I don’t mind using character count to break the text (preferable) because the text doesn’t change. The div containing this text is referenced by the id #npali

Output should look like this

Inquire about space<br>
Enter Library

I am unable to get this to work. I was wondering if a PHP alternative would be available to help me achieve this.

Please help if you might know the fix.

How to&Answers:

This can be done with the explode() See more: https://www.php.net/manual/en/function.explode.php

An example:

function slash2br($str){
  $parts = explode("/", $str);
  return implode("<br>", $parts);
}

You could also preg_replace() and replace the / with <br>.