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'.
Related
This question already has answers here:
implode() string, but also append the glue at the end
(6 answers)
Closed 1 year ago.
I have this sentence:
piece:5,dozen:10
and I need to print it via php like this:
piece:5$,dozen:10$
without editing the line in the editor, I need php to do it automatically. Now I thought to split it like this:
$uom = "piece:5,dozen:10";
$lst1 = explode(',', $uom);
var_dump($lst1);
and it returned like this:
array (size=2)
0 => string 'piece:5' (length=7)
1 => string 'dozen:10' (length=8)
which is ok for now, so I need after each string piece:5$ dozen:10$ and then I did this:
$lst2 = implode('$ ', $lst1);
echo $lst2;
It printed:
piece:5$ dozen:10
I need the $ to be printed also after 10, so it will be like this:
piece:5$ dozen:10$
What is the right way to do this? What did I do wrong? What if the values are dynamically coming from the database?
You can use a combination of explode, array_map and implode like so:
<?php
$uom = 'piece:5,dozen:10';
$add_dollars = array_map(function($e) {
return $e . '$';
}, explode(',', $uom));
echo implode(" ", $add_dollars);
I see few explode-implode answers. I don't want to duplicate answer, so let me do it another way – with regex.
$data = "piece:5,dozen:10";
echo preg_replace("/(\d+)/i", "$1\$", $data);
It may be a good idea to make it a bit more complex, i.e. take not only \d+, but also previous string and colon. Rather without(!) comma. In my opinion this may be better (because it's usually worth to be strict in regex):
$data = "piece:5,dozen:10";
echo preg_replace("/([a-zA-Z]:\d+)/i", "$1\$", $data);
I encourage you to read php manual: preg_replace.
Answering also the question about jQuery – you don't need to use jQuery, you can do that in pure javascript. And it will be really similar! For example:
let data = "piece:5,dozen:10";
let modified = data.replace(/([a-zAZ]:\d+)/g,"$1\$");
console.log(modified);
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 );
This question already has answers here:
Stripping html tags using php
(7 answers)
PHP removing html tags from string
(6 answers)
Closed 4 years ago.
value of $attributes['st_title'] is Physical Design Engineers
I want to remove and . The final expected output is: $attributes['st_title'] = Physical Design Engineers
I am trying as follows:
$pattern[0] = "/<a.[^>]+>/";
$pattern[1] = '/</a>/';
$replacement[1] = '';
$replacement[0] = '';
$attributes['st_title'] = preg_replace( $pattern, $replacement, $attributes['st_title'] );
But it sets $attributes['st_title'] as empty. Any idea?
Since you want text only from the link, so use strip_tags()
echo strip_tags($text);
https://eval.in/979039
Use php strip_tags()
or
preg_replace("/<.*?>/", "", $attributes['st_title']);
This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 7 years ago.
I'm new to php and have a basic question regarding parsing strings.
I have a variable "SKU" whose value is "9897_BLK"
I need to split this into two separate values:
"STYLE" with a value of "9897" AND "COLOR" with a value of "BLK"
I suppose there's a way to use the underscore to delimit the string.
Thanks for your help.
Try explode. This basically separates your string using a delimiter as your first parameter, and the string as the second parameter and returns an array of strings generated. Afterwards you can check if the string was properly parsed or you can just directly assign values just like the one below:
$sku = "9897_BLK";
$sku_parsed = explode("_", $sku);
$style = $sku_parsed[0];
$color = $sku_parsed[1];
If you want more details, the PHP manual is very accessible and has in-depth examples and use-cases for various scenario.
http://php.net/manual/en/function.explode.php
Try this:
$sku = "9897_BLK";
list($style, $color) = explode("_", trim($sku));
In PHP we use an function to split any string with delimiter.
You may try the explode function. The explode function return an array as output and the array values are the delimited values respectively.
Here is code snippet:
$SKU = "9897_BLK";
$DELIMITED_ARRAY = explode("_", $SKU);
$STYLE = $DELIMITED_ARRAY[0];
$COLOR = $DELIMITED_ARRAY[1];
This question already has answers here:
Php compare strings and return common values
(3 answers)
Closed 8 years ago.
I have two strings of keywords
$keystring1 = "tech,php,radio,love";
$keystring2 = "Mtn,huntung,php,tv,tech";
How do i do return keywords that common in both strings
You can do this:
$common = array_intersect(explode(",", $keystring1), explode(",", $keystring2));
If you want them back into strings, you can just implode it back.
Hmm, interesting question... You can use this.
$arr1 = explode(',',$keystring1);
$arr2 = explode(',',$keystring2);
$duplicates = array_intersect($arr1,$arr2);
foreach($duplicates as $word) {
echo $word;
}
You could explode() both strings on commas into arrays and loop through the first array checking to see if any of the words exist in the second array using the in_array() function. If so then add that word to a "common words" array.
Those are going to need to be arrays not variables.
$keystring1 = array('tech','php','radio','love');
$keystring2 = array('mtn','huntung','php','tv','tech');
First of all...