How to remove specific part from a String in php [duplicate] - php

This question already has answers here:
How can I remove part of a string in PHP? [closed]
(9 answers)
Closed 8 years ago.
I have the following String
Hi this string is to #removeStart be modified by #removeEnd function
And I want to remove the string part coincides by #removeStart and #removeEnd and these two tags from the above string

Try this
$str = 'Hi this string is to #removeStart be modified by #removeEnd function';
echo preg_replace('/#removeStart (.*) #removeEnd/','',$str);

Check this Demo Code Viper
Pattern Check
(#\b\w+)
PHP
<?php
$str="Hi this string is to #removeStart be modified by #removeEnd function";
echo preg_replace('(#\b\w+)','',$str);
?>
Result
Hi this string is to be modified by function

Related

Get part of string PHP [duplicate]

This question already has answers here:
PHP - parse current URL
(6 answers)
Closed 2 years ago.
How to get a part of string using PHP?
I have a string like this.
https://test-app.com/admin/api/2019-10/orders.json?limit=2&page_info=eyJpZHMiOiIyMDY3MTczMTEzOTg3LDIwNjU0ODU0MzA5MTUsMjA2NTQ3OTI3MDUzMSwyMDYyODE3MzI5MjgzIiwibGFzdF9pZCI6MjA2NTQ4NTQzMDkxNSwibGFzdF92YWx1ZSI6IjIwMjAtMDMtMTcgMTg6MTc6NTkiLCJkaXJlY3Rpb24iOiJuZXh0In0
I want only the link.. like this
orders.json?limit=2&page_info=eyJpZHMiOiIyMDY3MTczMTEzOTg3LDIwNjU0ODU0MzA5MTUsMjA2NTQ3OTI3MDUzMSwyMDYyODE3MzI5MjgzIiwibGFzdF9pZCI6MjA2NTQ4NTQzMDkxNSwibGFzdF92YWx1ZSI6IjIwMjAtMDMtMTcgMTg6MTc6NTkiLCJkaXJlY3Rpb24iOiJuZXh0In0
That's not just a string it's a URL so use URL and path functions:
$parts = parse_url($str);
echo basename($parts['path']).$parts['query'];

How Do I remove last comma from string in php [duplicate]

This question already has answers here:
Json_encode or remove last comma?
(3 answers)
How do I remove a comma off the end of a string? [duplicate]
(10 answers)
Closed 1 year ago.
I want to remove the last comma from below array.
{"location":"Park Town,Chennai,Tamil Nadu" },
{"location":"Arajar Salai,Simmakkal,Madurai" },
Jon Stirling have right in his comment. If you try to create json manually - it's bad idea.
But anwser to your question should be something like this (didn't test it) :
<?php
$tab=explode(",",$string);
array_pop($tab);
$string=implode(",",$tab).']';
?>
Just use php rtrim() function on the string you will get your result.
like below
<?php
$text = '{"location":"Park Town,Chennai,Tamil Nadu" },{"location":"Arajar Salai,Simmakkal,Madurai" },';
$test = rtrim($text,',');
echo $test;
?>
A live example https://eval.in/849038

PHP Replace text in string [duplicate]

This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 6 years ago.
i have a problem.
I want in php to replace all 'blablabla' with 'bla'(because i hate blablabla). Here's my code:
<?php
$string = 'Dracula always says BlaBlaBla but says he never says BlaBlaBla';
$result = answer to replace here
?>
Thanks
Use srt_replace function to do that like this :
str_replace("BlaBlaBla","bla",$string);
You can use something like this:
str_replace("BlaBlaBla","bla",$string)
Also you can find it here in the docs: http://php.net/manual/en/function.str-replace.php

Php, return portion of string before "0" [duplicate]

This question already has answers here:
How do I remove all specific characters at the end of a string in PHP?
(10 answers)
Closed 6 years ago.
I search PHP method for return portion of string before "0" has no number (1-9) after.
Example :
604000
I want to PHP script return :
604
Can you help me ?
Just use rtrim() as
$str="604000";
echo $str = rtrim($str, '0');

Using PHP to find the number inside {{2461245}} [duplicate]

This question already has answers here:
PHP Extract numbers from a string
(8 answers)
Closed 7 years ago.
What is the best way to extract the number in the following string:
"This is a test string {2461245} and I need to extract the number"
Thank you.
This should help you :
$myText = 'This is a test string {2461245} and I need to extract the number';
preg_replace("/[^0-9]/","",$myText);
What you are saying there is : I want to replace everything which is not a number in the myText variable by "", which means everything which will stay in the end is your number.

Categories