I am trying to fetch the first 2 words from a sting, the first word Animal and second word is (Cat) my current code does fetch it but the () brackets gets ignore I want to fetch exactly like on string Animal (Cat).
echo implode(' ', array_slice(str_word_count($_POST['str'], 2), 0, 5));
You can use
array_sliceto get the first two words and then useimplodeto join them:I get the output as:
Here's a sandbox: https://sandbox.onlinephpfunctions.com/c/a2c44