Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - replace string occurrences

Tags:

string

php

I have a string "First line | second line | third line" How can I replace | with a new line character?

I'm trying to use preg_replace but with no liuck

like image 836
Oleg Avatar asked Aug 15 '12 08:08

Oleg


People also ask

How can I replace part of a string in PHP?

The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.

How can I replace multiple characters in a string in PHP?

Approach 1: Using the str_replace() and str_split() functions in PHP. The str_replace() function is used to replace multiple characters in a string and it takes in three parameters. The first parameter is the array of characters to replace.

What is substr () in PHP and how it is used?

substr in PHP is a built-in function used to extract a part of the given string. The function returns the substring specified by the start and length parameter. It is supported by PHP 4 and above. Let us see how we can use substr() to cut a portion of the string.

What is use of trim in PHP?

Definition and Usage. The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.


1 Answers

here it is

str_replace('|',"\n",$string);

when \n is placed in double qouted string it changes to a new line

like image 98
EaterOfCode Avatar answered Oct 21 '22 04:10

EaterOfCode