Sujan Shrestha
Sujan Shrestha

Reputation: 614

How to detect a line break next to some string and remove it?

I have the description column in database and it contains the sample data as bleow

Product/Service: product
First Payment Amount: $50 USD
Recurring Amount: $10 USD

I want to replace the string First Payment Amount: $50 USD with blank so i used str_replace as below

$details = str_replace('First Payment Amount: $50 USD', '', $description);
echo $detsils;

but the result shows like this

Product/Service: product

Recurring Amount: $10 USD

there is a gap. it is because of the line break after the string First Payment Amount: $50 USD. how to detect the line breaks next to some string and remove it in such cases and get the result like this.

Product/Service: product 
Recurring Amount: $10 USD

Upvotes: 0

Views: 53

Answers (3)

Neelesh
Neelesh

Reputation: 193

try this one after str replace

$details =  preg_replace( "/\r|\n/", "", $description );

Upvotes: 0

yergo
yergo

Reputation: 4980

You can use preg_replace to work it at one catch:

$url = "Product/Service: product
First Payment Amount: $50 USD
Recurring Amount: $10 USD";

$url = preg_replace('/First Payment Amount\: \$[0-9]+ USD[\s]+/i', '', $url);

\s (docs here) is searching for whitespaces, newlines of both kind are whitespaces, so you dont mind operating system youre receiving strings from.

i at the end is not required, but I'm used to it. It turns patterns to match case insensitive.

Upvotes: 1

Bo Chen
Bo Chen

Reputation: 436

try below:

$details = str_replace('First Payment Amount: $50 USD\r\n', '', $details);

Upvotes: 0

Related Questions