preg_replace inside of PHP file - php

I have a file that i'm parsing And I'm trying to replace $mail["email_from"] = "test#example.com"; with $mail["email_from"] = request("email");,(meaning that I want to replace all the lines that has $mail["email_from"] at the begining an ; at the end) and here's my preg_replace:
$email = "$mail[\"email_from\"] = request(\"email\")";
$newcontent = preg_replace("/\$mail[\"email_from\"](.+);/",$email,$content);
What's the error in my code? and how to fix it? Much appreciated
DEMO

After using good quotes and escaping all chars you need, this works:
$email = '$mail["email_from"] = "test#example.com";';
$replacement = '$mail["email_from"] = request("email");';
$newContent = preg_replace('/\\$mail\\[\\"email_from\\"\\](.+);/i', $replacement, $email);
echo $newContent; //$mail["email_from"] = request("email");

$email = "$mail[\"email_from\"] = request(\"email\")";
^---double-quoted string
^^^^^---array reference
You probably need
$email = "\$mail[\"email_from\"] = request(\"email\")";
^--escape the $

Use ^ and $ to specify beginning and end of line. Special characters like $, [, and ] need to be escaped.
<?php
$content = '$mail["email_from"] = "test#example.com";';
$email = '$mail["email_from"] = request("email");';
$newcontent = preg_replace('/^\$mail\["email_from"\] =.+;$/',$email,$content);
echo $newcontent . "\n";
outputs:
$mail["email_from"] = request("email");

Related

PHP Hiding Last 3 Chars Every Word

Example String:
AAAAAA BBBBB CCCCCCC
Output:
AAAXXX BBXXX CCCCXXX
I need to hide every last 3 chars of words. I tried str_replace but I can't make it. Thanks for help
$replacement = "***";
if (stripos($name, ' ') !== false) {
$star = substr($name, 0, -3).$replacement;
}
I tried this but code only hide last word's 3 char. I need every word. Thanks
You need to split your string up, replace the last three characters, and then reassemble it.
$replacement = "***";
// break your string into an array based on the spaces
$temp = explode(' ', $name) ;
// our temporary name
$newName = '' ;
// loop through each part of the original name
foreach($temp as $section) {
// append our modified string along with a space
$newName .= substr($section, 0, -3).$replacement . ' ' ;
}
// set $name = $newName without the trailing space
$name = substr($newName,0,-1) ;
$replacement = "***";
$star = "";
$pieces = explode(" ", $name);
foreach ($pieces as $piece){
$star .= substr($piece, 0, -3).$replacement." ";
}
Try this function,
function replaceLastThreeChars($string){
$reversed_string = strrev($string);
$replaced_string = str_replace(substr($reversed_string,0,3),"XXX",$reversed_string);
return strrev($replaced_string);
}
If you have a sentence, then you can slice it using spaces and call to this function and finally recreate the sentence.

PHP pred_split Regex expression to return html tags within a sub-string

I have read many threads on this regex question, but none of them seem to work. I am sure it's a result of me really not understanding regex expressions.
Let's say I have the following string:
string='<i>he<a herf="http://www.cnn.com">ll</b>o</i>'
I would like to grab the html tags buried in the substring "hello". I am using the following php function:
$temp = preg_split('/[0-9A-Za-z]+</', $string);
What I am looking for is an array with the following:
a herf="http://www.cnn.com">, and /b>
I can tack on the leading '<'. My results, using the above regex in my preg_split call seem to be including the first '' tag
My full code:
$string = '<i>he<a herf="http://www.cnn.com">ll</b>o</i>';
$temp = preg_split('/[0-9A-Za-z]+</', $string);
echo('<pre>');print_r($temp);echo('</pre>');
$num = count($temp);
$counter = 1;
foreach($temp as $key=>$tag_stem){
if($counter<$num) {
echo('<xmp>');print_r('tag_stem = ' . $tag_stem);echo('</xmp>');
$temp_tag = '<' . $tag_stem;
echo('<xmp>');print_r('temp tag = ' . $temp_tag);echo('</xmp>');
if (empty($temp2)) {
$temp2 = str_replace($temp_tag, '', $string);
} else {
$temp2 = str_replace($temp_tag, '', $temp2);
}
echo('<xmp>');print_r('string = ' . $temp2);echo('</xmp>');
if (strstr($temp_tag, '</')) {
$temp2 = $temp2 . $temp_tag;
} else {
$temp2 = $temp_tag . $temp2;
}
echo('<xmp>');print_r("new string = " . $temp2);echo('</xmp>');
}
$counter++;
}
$temp_array = explode($word, $string);
echo('<xmp>');print_r("final string = " . $temp2);echo('</xmp>');
My results are as follows:
tag_stem = <i>
temp tag = <<i>
string = <i>he<a herf="http://www.cnn.com">ll</b>o</i>
new string = <<i><i>he<a herf="http://www.cnn.com">ll</b>o</i>
tag_stem = a herf="http://www.cnn.com">
temp tag = <a herf="http://www.cnn.com">
string = <<i><i>hell</b>o</i>
new string = <a herf="http://www.cnn.com"><<i><i>hell</b>o</i>
tag_stem = /b>
temp tag = </b>
string = <a herf="http://www.cnn.com"><<i><i>hello</i>
new string = <a herf="http://www.cnn.com"><<i><i>hello</i></b>
final string = <a herf="http://www.cnn.com"><<i><i>hello</i></b>
Not the first iteration. For whatever reason, it's picking up the first "<i>".

php eval condition understanding

I am learning PHP. I see a code including "eval" below:
<?php
$name = 'cup';
$string = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$cup = \"$str\";");
echo $cup. "\n";
?>
My question is about "\$cup = \"$str\";" . Why do I have to use \, " and ; to run the above code? Simply eval($cup = $str) gives me error.
Simply eval($cup = $str) is wrog, eval need a string, eg;
eval('$cup = $str;'); // this work
now, if use double quote, eg
eval("\$cup = \"$str\";");
in double quote strings variables in the strings will be evaluated. eg:
$cup = 'hello';
$str = 'world';
eval("$cup = \"$str\";"); // eval("hello = \"world\";");
\is a escape characters
$cup = 'hello';
$str = 'world';
eval("\$cup = \"$str\";"); // eval("$cup = \"world\";");
recommended reading: PHP Strings
Side note: PHP eval is evil (use very, very careful)

Removing Certain Text from String in PHP

I am trying to trim a string in PHP so that I can only get certain text from the String.
I have an email stored to a String for instance some_name#somedomain.com .
How can I remove the text after the '#' so that I would only 'some_name'?
In PHP you can do :
$string = 'some_name#somedomain.com';
$res = explode('#', $string);
echo $res[0];
Or you can use regexp, string functions in php ... etc
You should know both ways to do this:
substr
$mail = "some_name#somedomain.com";
echo substr($mail, 0, strpos($mail, '#') );
explode
list($name, $domain) = explode('#', $mail);
echo $name;
If you don't need the $domain you can skip it:
list($name) = explode('#', $mail);
More about list.
Demo: http://ideone.com/lbvQF
$str = 'some_name#somedomain.com';
$strpos = strpos($str, "#");
echo $email = substr($str, 0,$strpos);
you can try this to get string before #
Try This
$str1 = "Hello World";
echo trim($str1,"World");
You could try split using regex and the # symbol. This will return two Strings which you can then use just to acquire the 'some_name'.
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
String s = "some_name#somedomain.com";
String name = s.substring(0,s.indexOf("#");

remove un-wanted characters in email using preg-replace

I want to sanitize the email a#$%#$#b##$#$2344324.com to a#b.com .
I tried and failed
echo filter_var("a#$%#$#b##$#$2344324.com", FILTER_SANITIZE_EMAIL); //result: a#$%#$#b##$#$2344324.com
I need to trim special characters in a email(sanitize to remove special characters). I used below code but I was unsuccessful.
$string = preg_replace("/^[a-zA-Z0-9._%+-]+#(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/", "", "a#$%#$#b##$#$2344324.com");
echo $string;//result: a#b.com -- unwanted characters trimmed here.
There is already a RFC-based solution here: http://fightingforalostcause.net/misc/2006/compare-email-regex.php
function is_valid_email_address($email){
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$quoted_pair = '\\x5c[\\x00-\\x7f]';
$domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
$quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
$domain_ref = $atom;
$sub_domain = "($domain_ref|$domain_literal)";
$word = "($atom|$quoted_string)";
$domain = "$sub_domain(\\x2e$sub_domain)*";
$local_part = "$word(\\x2e$word)*";
$addr_spec = "$local_part\\x40$domain";
return preg_match("!^$addr_spec$!", $email) ? true : false;
}

Categories