PHP Replace text in string [duplicate] - php

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

Related

How to put specific string in into array? [duplicate]

This question already has answers here:
PHP: Best way to extract text within parenthesis?
(8 answers)
Closed 3 years ago.
I have a string like this:
[Rise] and rise [again] until lambs become [lions].
I want to get all the string inside the [] (Rise, again, lions) and put it in to an array. How can I do this?
Edit: Thank you guys, I already found the solution here: PHP: Best way to extract text within parenthesis?
You can do it with regular expressions:
$s = '[Rise] and rise [again] until lambs become [lions].';
preg_match_all('/\[([^\]]+)\]/', $s, $m);
print_r($m[1]);

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

Remove url from string using PHP [duplicate]

This question already has answers here:
Get path from URL
(2 answers)
Closed 8 years ago.
Can anyone please tell me how to remove url from a string using PHP
I have this string
src="http://cdn1.vox-cdn.com/uploads/chorus_image/image/34918177/layering__1_.0_standard_90.0.png"
Desired Output:
src="/uploads/chorus_image/image/34918177/layering__1_.0_standard_90.0.png"
You can use the parse_url() buil-in php function for this.
For example:
<?php
$url = 'http://cdn1.vox-cdn.com/uploads/chorus_image/image/34918177/layering__1_.0_standard_90.0.png';
$parsed_url = parse_url($url);
var_dump($parsed_url);
?>
Output:
So, after the url is parsed, the bit you want is found on $parsed_url['path']. Hope this helps.

How to extract last part in PHP? [duplicate]

This question already has answers here:
How do I extract query parameters from a URL string in PHP?
(3 answers)
Closed 8 years ago.
This would be an example:
redirect
dynamic_word can be changed because it is dynamic. When click "redirect", dynamic_word will be extracted. So, how to extract it in redirect.php file ? Thanks !
Use $_GET to get parameters from an URL
<?php
$thatName = $_GET['q'];
echo $thatName;
Result
dynamic_word
If samitha's correct looking answer is incorrect then perhaps you mean you would like to extract the dynamic word from a string.
In that case you could do
<?php
$string = 'http://mywebsite.com/redirect.php&q=dynamic_word';
$ex_stirng = explode('&q=', $string);
$dynamic_word = $ex_string(1);
?>
Or even use the strstr function:
http://www.php.net/manual/en/function.strstr.php

If /store/*/aproduct [duplicate]

This question already has answers here:
If url is /store/*
(3 answers)
Closed 8 years ago.
if($_SERVER['REQUEST_URI']=='/store/'.*.'/aproduct')
Is that possible? So if I type /store/something/aproduct or /store/abc123/aproduct the if statement happens? The '*' should be everything. Dosent matter if you type cow, 123, lol or youarenice
Try with:
if ( preg_match('~^/store/[^/]+/aproduct$~', $_SERVER['REQUEST_URI']) )
yes, you can use regular expressions for that. Something like this should work:
if(preg_match('#^\/store\/.+\/aproduct#', $_SERVER['REQUEST_URI'])) {
// do something
}

Categories