Replace occurences of string corresponding to a regex - php

I have the following strings:
$a = "test1";
$b = "test 2";
$c = "test<3";
$d = "test&4";
I would like to replace occurrences of "&" followed by some letters and terminate by a ";".
The output should be :
$a = "test1";
$b = "test 2";
$c = "test 3";
$d = "test&4";
How can I do that with PHP?

In this particular case, you don't need a regex, most likely what you need is to decode the HTML entities, and that can be done with html_entity_decode(), as in:
$a = html_entity_decode("test1");
$b = html_entity_decode("test 2");
$c = html_entity_decode("test<3");
$d = html_entity_decode("test&4");
var_dump($a,$b,$c,$d);

Use this:
$x = preg_replace('/&[a-z]+;/', ' ', $b);
echo $x;

The answer of #this.lau_ is the best, but if you want the regexp, try this
(\&)([a-z]{1,4})(;)

Related

Concatenate two different variable in PHP

I have the following code
<?php
$str="3dollars";
$a=20;
$a+=$str;
print($a);
?>
How can be the Output of the above program is 23 ?
Thanks In Advance !!
The + operator will coerce the string into being an integer, so it will (internally) do something like this:
$str = "3dollars";
$a = 20;
$a += $str;
// $str = (int)"3dollars";
// $str = 3;
$a = 23;
What you want to do is use the 'concatenation' operator (.):
<?php
$str = "3dollars";
$a = 20;
$a .= $str;
print($a); // 203dollars
It's taking "3dollars" as a number, getting $str = 3.
And when you echo, you add 20, to $str, so it prints 23 and $a = 23.
<?php $a += $str;
print($a);
it echo 23; //$a=$a+$b;?>
Use this
<?php
$a = 20;
$str = "3dollars";
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
$matches = $a + $matches[0][0];
echo '<br> Value Is :'.$matches;
?>
This will print 23 as Answer.
Output
Array ( [0] => Array ( [0] => 3 ) )
Value Is :23
phpfiddle Preview
$str= (int) "3dollars";
$a=20;
$a+=$str;
print($a);
Use (int) to convert string to integer.

Each value of a string to a separate array conversion

I have a html page contents, that I converted into a string separated by "#".
Example:
(2R)-2-hydroxy#250.181#C15H24NO2#2#1#46#1#11#1.1266#1#18#6
Is there any way to convert each value of these string to convert into a array?
I need an output like this:
$a = (2R)-2-hydroxy
$b = 250.181
$c = C15H24NO2
$d = 2
$e = 1
//etc...
This should work for you:
Just explode() your string and loop through the array. Then you can assign each value to a variable, where you can increment the character.
$str = "(2R)-2-hydroxy#250.181#C15H24NO2#2#1#46#1#11#1.1266#1#18#6";
$arr = explode("#", $str);
$start = "a";
foreach($arr as $v) {
$$start = $v;
$start++;
}
This should work
$array = explode("#", "(2R)-2-hydroxy#250.181#C15H24NO2#2#1#46#1#11#1.1266#1#18#6");
$array [0] = (2R)-2-hydroxy
$array [1] = 250.181
...

Unable to concat physical path of a file and file name in php

Here comes a problem when i about to concat the following
string 1 = 'C:\xampp\htdocs\packiya\new_iiser\assets\uploads\banner'
string 2 = '\'
string 3 = 'file_name.ext'
I was unable to concat these three strings.
What problems are you facing? A simple concatination like this should work:
$concatenatedString = $string1.$string2.$string3
Its very simple,for e.g
<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"
$a = "Hello ";
$a .= "World!"; // now $a contains "Hello World!"
?>

Add +1 to a string obtained from another site

I have a string I get from a website.
A portion of the string is "X2" I want to add +1 to 2.
The entire string I get is:
20120815_00_X2
What I want is to add the "X2" +1 until "20120815_00_X13"
You can do :
$string = '20120815_00_X2';
$concat = substr($string, 0, -1);
$num = (integer) substr($string, -1);
$incremented = $concat . ($num + 1);
echo $incremented;
For more informations about substr() see => documentation
You want to find the number at the end of your string and capture it, test for a maximum value of 12 and add one if that's the case, so your pattern would look something like:
/(\d+)$/ // get all digits at the end
and the whole expression:
$new = preg_replace('/(\d+)$/e', "($1 < 13) ? ($1 + 1) : $1", $original);
I have used the e modifier so that the replacement expression will be evaluated as php code.
See the working example at CodePad.
This solution works (no matter what the number after X is):
function myCustomAdd($string)
{
$original = $string;
$new = explode('_',$original);
$a = end($new);
$b = preg_replace("/[^0-9,.]/", "", $a);
$c = $b + 1;
$letters = preg_replace("/[^a-zA-Z,.]/", '', $a);
$d = $new[0].'_'.$new[1].'_'.$letters.$c;
return $d;
}
var_dump(myCustomAdd("20120815_00_X13"));
Output:
string(15) "20120815_00_X14"

How to select start of a string until a :

I have this string:
467:some-text-here-1786
How can I select only the first numerical value before the ":" ?
Thank you
Very simple:
list($var) = explode(":",$input);
or
$tmp = explode(":",$input);
$var = array_shift($tmp);
or (as pointed out by PhpMyCoder)
$tmp = current(explode(":",$input));
$string = '467:some-text-here-1786';
$var = (int)$string;
Since you're extracting a number, this is enough :)
For an explanation of why this works, check the official PHP manual: http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
It's also really fast and really safe: you are sure you get a number.
$a = "467:some-text-here-1786";
$a = explode(":", $a);
$a = $a[0];
another way to do this:
$length = strpos($string, ':') + 1;
$number = substr($string, 0, $length);

Categories