0

I have this code for parsing RSS to HTML.

<?php $channel_desc = $channel->getElementsByTagName('description') ->item(0) ->childNodes ->item(0) ->nodeValue; 

I want to get only 150 characters from the first description. How can I limit the length output?

2
  • limit what? nodeValue? Add the xml url, that would be better to understand. Commented Mar 21, 2018 at 2:28
  • What do you mean by "the length output"? Do you mean "the length of the output"? Or something else? Can you elaborate? Commented Mar 15, 2024 at 20:38

2 Answers 2

1

You can use the "substr" function of PHP to extract the first 150 characters of any given string.

$channel_desc = substr($channel->getElementsByTagName('description') ->item(0)->childNodes->item(0)->nodeValue,0,150); 

Refer to substr.

Sign up to request clarification or add additional context in comments.

Comments

1

Use:

<?php // Single-byte strings $channel_desc = substr($channel_desc, 0, 150); // Multi-byte strings (UTF-8 support - multi-language support) $channel_desc = mb_substr($channel_desc, 0, 150); 

You can use the substr PHP function to trim the string length. 0 is the starting position and 150 is the length.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.