This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 2 years ago.
i have file names strings like this :
Admin_studies.php
Studies_lang.php
and i want to replace it like this :
Admin_classes.php
Classes_lang.php
which keep the case as it's and just replace the sting word
i tried something like this , but it will not keep the original chars cases
$newname = "classes";
$oldname = "studies";
$file_name = "Admin_studies.php"; //or may be $file_name = "Studies_lang.php";
echo preg_replace("/($oldname)/i","$1",$file_name );
You can set arrays for replacement templates
$newnames = ['classes', 'Classes'];
$oldnames = ['studies', 'Studies'];
$file_name = "Admin_Studies.php";
echo str_replace( $oldnames, $newnames, $file_name );
Related
This question already has answers here:
PHP: Split string [duplicate]
(7 answers)
Closed 2 years ago.
I have values stored in a database that look like this
10_wc_asis
I need to isolate the values between the underscores (_) like this:
Value 1 = 10
Value 2 = wc
Value 3 = asis
How can I do that in PHP?
Try explode :
$str = "10_wc_asis";
$pieces = explode("_", $str);
var_dump($pieces)
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 7 years ago.
What is the correct form to make $id behave as a PHP variable inside the str_replace command? I've tried wrapping the $id inside with {, or ., but nothing helped. I'm not even sure how to define the problem I'm having here so I didn't really know how to Google this:
$id="Something";
$new = str_replace('?abc', '?id=$id&abc', $original);
There are two ways to concatenate strings in PHP. In your example, it would be either:
$new = str_replace('?abc', '?id=' . $id . '&abc', $original);
or
$new = str_replace('?abc', "?id=$id&abc", $original);
Note that the first option is slightly more efficient, and the spaces are optional.
i think it needs to be work like this.
$search = 's';
$replace = 'r';
$subject = 'subject';
$result = str_replace($search,$replace,$subject);
var_dump($result);
and the result will be 'rubject'.
This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 7 years ago.
I'm writing a PhP script, but for a filter I need to search for a string in a string.
this is the script I have at the moment:
<?php
$stringtocheck = "Schiphol-Almere C./Hilversum/Utrecht C.";
$searchfor = "Hilversum";
// do the magic stuff over here
?>
How do i get the script to search for '$searchfor' in '$stringtocheck'?
Use strpos for to find the needle-string in the haystack-string. And here the haystack-string is stringtocheck and the needle-string is searchfor.
Example of using the function:
<?php
$stringtocheck = "Schiphol-Almere C./Hilversum/Utrecht C.";
$searchfor = "Hilversum";
if(strpos($stringtocheck, $searchfor) !== false)
echo true;
?>
This question already has answers here:
How to get a file's extension in PHP?
(31 answers)
Closed 9 years ago.
I have string "katrina.bhuvnesh.jpg" , I want to get jpg that means substring that matches the last dot(.)
I looked in substr() function but it's not working exactly what I need.
Please give me any function of PHP that matches string from last and returns substring from that point.
Thanks!!
Use pathinfo() like this:
$extension = pathinfo("katrina.bhuvnesh.jpg", PATHINFO_EXTENSION);
You can use 2 function calls:
$filename = 'somefile.sometext.jpg';
$type = end(explode('.', $filename));
// $type is now 'jpg'
Alternatively, you can use substr:
$type = substr($filename, strrpos($filename, '.'));
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP preg_match_all: Extract comma seperated list
I have a string that looks like: [something ="1,2,3"]
I need get everything between the quotes into a string. I'm sure that preg_match is the way to do this but i'm not sure of the expression to use.
$content = [something = "1,2,3"];
$new_string = preg_match('?regex?','$content');
Try this:
$content = '[something = "1,2,3"]';
if (preg_match('/"([^"]+)"/', $content, $matches)) {
$matchedContent = $matches[1];
}