Using eval() to execute string of PHP that contains SimpleHTML - php

I'm trying to use eval() to execute a string of SimpleHTML. I'm fully aware of the dangers of eval() and will not be using any user input for the string that is to be executed.
$my_data = str_get_html('<html><body>Hello!</body></html>');
$str = '$my_data->find(\'a\', 0)->attr[\'href\']';
eval ("\$str = \"$str\";");
echo $str;
The above code doesn't execute, and after echoing $str, I get:
('a', 0)->attr['href']
What happened to the first part of the $str string (i.e. $my_data->find )? How can I actually execute the code from the $str string?

The code you are passing to the eval is wrong. You are trying to eval the following code:
$str = "$my_data->find('a', 0)->attr['href']";
The correct code would be:
$str = $my_data->find('a', 0)->attr['href'];
Or:
$str = "{$my_data->find('a', 0)->attr['href']}";
This code works:
<?php
require __DIR__ . '/simplehtmldom_1_9_1/simple_html_dom.php';
$my_data = str_get_html('<html><body>Hello!</body></html>');
$str = '$my_data->find(\'a\', 0)->attr[\'href\']';
eval ("\$str = $str;");
echo $str;

Related

Ansvered / How to use content from database in PHP functions?

I have a PHP function which converts #Hashtag into a link...
function convertHashtags($str) {
$regex = "/#+([a-zA-Z0-9_]+)/";
$str = preg_replace($regex, '$0', $str);
return($str);
}
It work properly when I use it with a common string
$string = "Hello #World";
$string = convertHashtags($string);
(in this case an output would be: Hello #World
But when I'm trying to insert something from my database to that string it displays, but without that function's effect…
$string = $row["content"];
$string = convertHashtags($string);
(an output: Hello #World)
I am new to the PHP and MySQL stuff… Certainly, there are many things I don't know yet :D
What's wrong with this function?
Thanks!
function convertHashtags($str){
list($str1, $str2) = explode("#", $str) ;
$str2 = '#'.$str2.'';
$str = $str1." ".$str2 ;
return($str);
}
Can you use the above function and test with database entry?
Oh well, I just add new element to a database and it works!
I was testing it on old elements, they was inserted before I wrote the function.
I should try it before writing a this, my bad… thanks for help anyway!
$string = $row["content"];
$string = (string)$string;
$string = convertHashtags($string);
Use the above code and it will work.

Adding something to the start of a variable?

I am looking for some code that allows you to add +44 onto the beginning of my $string variable.
So the ending product would be:
$string = 071111111111
+44071111111111
Your $string variable isn't actually a string in this scenario; it's an integer. Make it a string by putting quotes around it:
$string = "071111111111"
Then you can use the . operator to append one string to another, so you could do this:
$string = "+44" . $string
Now $string is +44071111111111. You can read more about how to use the . (string concatenation operator) on the PHP documentation here.
Other people's suggestions of just keeping $string as an integer wouldn't work: "+44" . 071111111111 is actually +447669584457. Due to the 0 at the start of the number, PHP converts it to an octal number rather than a decimal one.
You can combine strings by .
$string = '+44'.$string;
You can use universal code, which works with another parameters too.
<?php
$code = "+44";
$string = "071111111111";
function prepend(& $string, $code) {
$test = substr_replace($string, $code, 0, 0);
echo $test;
}
prepend($string, $code);
?>

PHP mb_eregi_replace does not work

I am trying to match a whole UTF-8 word in PHP. This is how I am trying to do it:
<?php
$string = 'DS DAMAT TAKIM ELBİSE (GOLD)';
$search = 'takım elbise';
$replace = 'TakımElbise';
$result = mb_eregi_replace('/\b'.$search.'\b/ui', $replace, $string);
echo $result;
echo preg_match('/\b'.$search.'\b/ui', $replace);
?>
But it does not work. What can be the problem?
NOTE:
I have tried adding these lines at the beginning of script:
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');
No result.
How about:
$string = 'DS DAMAT TAKIM ELBİSE (GOLD)';
// ^__ this isn't an I
$search = 'takım elbİse';
// ^__ this isn't an I
$replace = 'TakımElbise';
$result = preg_replace("/\b$search\b/ui", $replace, $string);
echo $result;
I've just change the i to İ in the search string. You may want to use lowercase (I haven't on my keyboard)
See the comment here: http://php.net/manual/en/function.mb-ereg-replace.php
Unlike preg_replace, mb_ereg_replace doesn't use separators
Example with preg_replace:
$data = preg_replace("/[^A-Za-z0-9\.\-]/","",$data);
Example with mb_ereg_replace:
$data = mb_ereg_replace("[^A-Za-z0-9\.\-]","",$data);
Also, don't use the ui flags.

eval() function in php

here is a code where I don't understand why the php code where the output is: This is a $string with my $name in it. This is a cup with my coffee in it.
<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
// will not echo the value of the strings variable because there in ' '
echo $str. "\n";
// this function is like writing the php code outside of it
// it gets a string with php statments (;)
// because the code is written in a string
// if it is written it double quotes you have to escape $ and "
// and if it is written in single quotes you have to escape '
eval("\$str = \"$str\";");
//it is not like this, why?????
//eval('$str = "$str";');
// and not like this, why???????
//$str = "$str" ;
echo $str. "\n";
?>
why doesn't the statement : eval('$str = "$str";'); or the statement: $str = "$str" ; do the same thing as the statement: eval("\$str = \"$str\";"); in this code
A Double quoted string evaluates all the variables inside it. A Single Quoted String does not.
Now to this statement
eval("\$str = \"$str\";");
first \$str -> the $ is escaped, so its a literal, and not the $str variable
second $str -> the $ is not escaped and the whole string is in double quotes, so this will become
$str = "This is a $string with my $name in it."
Now this PHP code is evaluated, which assigns the string on right to the variable on left. Hence $str becomes what This is a cup with my coffee in it.
Eval should be avoided.
//it is not like this, why?????
//eval('$str = "$str";');
Because the input string might contain single quotes, so you can't use them to start and end the string.
// and not like this, why???????
//$str = "$str" ;
Because you want to evaluate a string, and the above is no string.
I don't see the point of this example, just use double quotes:
<?php
$string = 'cup';
$name = 'coffee';
$str = "This is a $string with my $name in it.";
echo $str. "\n";
?>
In the first eval statement:
eval("\$str = \"$str\";");
As second $ is not escaped, and you are using double quotes over the entire arguement, so second $str's value is passed to the eval, and the argument of eval becomes:
eval("\$str = \"This is a $string with my $name in it.\";");
which when evaluated, becomes:
$str = "This is a $string with my $name in it.";
Which assigns 'This is a cup with my coffee in it.' to $str.
In the second eval:
eval('$str = "$str";');
the statement evaluated is:
$str = "$str";
Which is same as your third statement. When this statement is executed, it converts non-strings to strings. In this case, $str is already a string, so this statement has no effect on the value of $str.
Hope this helps. :)
Why would you need eval in this context ?
Variables inside single quotes will not be interpreted , Instead put it under double quotes.
$str = "This is a $string with my $name in it."; //<--- Replaced single quotes to double quotes.
Secondly.. If you are really worried about escaping why don't you make use of a HEREDOC Syntax
<?php
$string = 'cup';
$name = 'coffee';
$cont=<<<ANYCONTENT
This is a $string with my $name in it. This text can contain single quotes like this ' and also double quotes " too.
ANYCONTENT;
echo $cont;
OUTPUT :
This is a cup with my coffee in it. This text can contain single quotes like this ' and also double quotes " too.

Split php string and keep delimiter with the first output

I'm trying to split a php string in to parts the first one include the delimiter but the second one doesn't
$string = "abc==123";
What I want exactly is to get
$string['0'] = "abc==";
$string['1'] = "123";
Thanks for help
Simple enough
<?php
$string = explode("==", $string);
$string[0] .= "==";
?>
I believe you want the function strstr
http://us1.php.net/strstr
You can use PHP's explode function,
$data = "abc==123";
$result = explode("==", $data);
echo $result[0]. "==";
echo $result[1];

Categories