Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Cut String At specific character

Tags:

php

character

$string = "aaa, bbb, ccc, ddd, eee, fff";

I would like to cut string after third , so i would like to get output from string:

aaa, bbb, ccc
like image 717
user1949029 Avatar asked Jan 30 '13 10:01

user1949029


People also ask

How do I cut a string after a specific character in PHP?

The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.

How do I remove a word from a string in PHP?

Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.

How do I remove all characters from a string after a specific character?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.

How can I split a string into two parts in PHP?

The str_split() function splits a string into an array.


1 Answers

$string = "aaa, bbb, ccc, ddd, eee, fff";
$arr = explode(", ", $string);
$arr = array_splice($arr, 0, 3);
$string = implode($arr, ", ");

echo $string; // = "aaa, bbb, ccc"
like image 80
Parimal Raj Avatar answered Sep 27 '22 18:09

Parimal Raj