This question already has answers here:
preg_replace: add number after backreference
(2 answers)
Closed 6 years ago.
I referred this question - Capture group reference + numeral
I tried with both the options provided there. Here is example code using ECMAScript expression (I guess I need to use this, correct me if I am wrong) -
$string = "box.last_rollout_revision = dummy";
$regex = "/(box\.last_rollout_revision[\s]*=[\s]*)[a-z0-9-_]*([\n]*)/";
$test = "234";
$replacementPattern = "$1".$test."$2";
echo preg_replace($regex,$replacementPattern,$string);
Expected output
box.last_rollout_revision = 234
Actual getting
34
P.S. I am trying to replace value against a key in a config file which has several other key-value pairs. I think regex will make it easier in this case. Correct me if I am wrong.
Change the replacement pattern to
$replacementPattern = '${1}'.$test.'$2';
See the IDEONE demo
Related
This question already has answers here:
Getting the first character of a string with $str[0]
(9 answers)
Closed 2 years ago.
I'm using the following code to display only the last name first character on my Wordpress site:
$name = $comment->comment_author;
$separate = mb_split(" ", $name);
$last = array_pop($separate);
echo implode(' ', $separate)." ".$last[0].".";
It works great with English names, but $last[0] will return a question mark when used with a foreign language (e.g Arabic, Hebrew, Greek etc). For example:
Name: השם שלי
Will return:
השם ?.
I've been trying to fix this for an hour now, but nothing so far.
Any idea?
You can try to set the encoding (https://www.php.net/manual/ro/function.mb-internal-encoding.php) before using the mb_split function.
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]);
This question already has answers here:
PHP: Variables in a Postgresql Query
(2 answers)
Closed 3 years ago.
I’m working on a project where I need to use postgresql to update info. I need to take
Martin’s chik ‘n’ chips
And make change it to
Martin\’s chik \’n\’ chips
How would I do this? I’ve looked at other posts, and found out to use substr() to create the new string and strpos() to find the ‘s, and even setting a new variable to keep the position of the previous ‘
Edit: thanks everyone, clearly didn’t do enough research!
If in PHP:
Check out str_replace(). e.g.
$text = "Martin’s chik ‘n’ chips";
$apostrophe = array("'","`","‘","’");
$newtext = str_replace($apostrophe,"\'",$text);
In this specific example, if you don't have any of the 'fancy' apostrophes, check out addslashes() as this will solve everything for you
This question already has answers here:
What kind of data format is that?
(2 answers)
Closed 4 years ago.
I'm having a hard time with this....
So, I have a load of text:
a:13:{s:9:"live_odds";i:0;s:14:"show_matchbook";i:0;s:12:"show_betfred";i:1;s:16:"show_williamhill";i:1;s:12:"betfair_show";i:1;s:16:"betfair_username";N;s:16:"betfair_password";s:9:"";s:20:"betfair_affiliate_id";s:3:"888";
Now, what I am trying to do is search for betfair_affiliate_id";s:3: within that bulk of text and then display the text between the speech marks.
I was trying
$betfred_show = 'betfair_affiliate_id";s:3:"';
$betfred_show_value = substr($string, strpos($string, $betfred_show) + strlen($betfred_show), 3);
which does work, it brings back 888... but it's not really future proof as this just gets the 3 next digits. However, these could change to 4 or 5 digits.
Any suggestions?
It is a serialised array. You can simply unserialize it and access the key
$array = unserialize($input);
$output = $array['betfair_affiliate_id'];
This question already has answers here:
php trim(), work poorly
(2 answers)
Closed 9 years ago.
$individual_file["uri"] = "public://iStock_000000527255XSmall.jpg";
print_r(ltrim($individual_file["uri"], "public://"));
Result -: Stock_000000527255XSmall.jpg
Why the missing i? But when my character starts with si, I get si in the result. Why does trim behave differently?
$individual_file["uri"] = "public://siStock_000000527255XSmall.jpg";
print_r(ltrim($individual_file["uri"], "public://"));
Result -: siStock_000000527255XSmall.jpg
It's because charlist is literally a list of single characters to remove from the left side of the string and i is listed in public://. Any character that falls in this list will be removed, no matter the order.
Ref: http://php.net/manual/en/function.ltrim.php
In fact this:
$individual_file["uri"] = "public://iStock_000000527255XSmall.jpg";
print_r(ltrim($individual_file["uri"], "publc://"));
would output:
ic://iStock_000000527255XSmall.jpg
Another example by changing the order:
$individual_file["uri"] = "public://iStock_000000527255XSmall.jpg";
print_r(ltrim($individual_file["uri"], "bcilpu:/"));
would output:
Stock_000000527255XSmall.jpg