PHP: Replace php code in string [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want replace php codes in string and running fro server
$x = 1;
$string = "OK:{first}Yes{second}No";
I want replace if($x == 1){ to {first} and } else { to {second}
After run and echo $string , I want result in html :
OK:Yes

How did you come up with this approach? Why not simply:
$string = $x == 1 ? 'OK:Yes' : 'OK:No';
That would get you the string you want based on the value of $x.
Also if you literally mean that you want to do a search-n-replace on the PHP code itself (as #OllyTenerife assumes), then are you going to write it into a file to be executed later, or use eval() or something? Doesn't sound like the right track there... In which case, remodel your code.

$string = str_replace("{first}","if($x == 1)",$string);
$string = str_replace("{second}","} else {",$string);

Related

I want to replace two separate words with only one word in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
I want to replace two words in a sentence with one word, like in the following example:
Kippa's luck was very bad yesterday.
I want to change it to:
Kippa's was very misfortune yesterday.
In this example, I know it has no sense, but it's just an example,
I want to replace (luck & bad) with the word (misfortune) in php.
Thank you
You can pass arrays as parameters to str_replace
<?php
$youText = "Kippa's luck was very bad yesterday.";
$replace_1 = ["bad", "luck"];
$replace_2 = ["misfortune", ""];
$result = str_replace($replace_1, $replace_2, $youText);
echo $result;
?>
Thank you. but actually there are alot of different words to be changed with, not only one word. I need something like this: **
$URLContent = "he has a bad luck. but he is ok, a good man";
$remove_multi = array(['bad', 'luck'],
['good', 'man']);
$replace_multi = array(['misfortune', ''],
['nice', '']);
$URLContent = str_replace($remove_multi, $replace_multi, $URLContent);

count amount of numbers in a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a array of random generated strings looking like this:
63njpn5u
byrtg1za
ht6wnz39
em1yyrju
2ytoxfxl
n5kaho14
zg92pg4n
gr9e7i01
u3t07ai4
I need a php code that will cycle trough these and output the ones back that contain one number. So in the example above I would've gotten back:
2ytoxfxl
em1yyrju
byrtg1za
I've tried using preg_match_all but I cant figure out how to use this with a array.
What you need is preg_grep:
print_r(preg_grep('/^\D*\d\D*$/', $your_array));
You can count like using string method :
$string = "2ytoxfx3l";
echo $int = strlen(intval(preg_replace('/[^0-9]+/', '', $string), 10));
Group of element:
$string = array( "63njpn5u","byrtg1za","ht6wnz39");
foreach($string as $str){
echo strlen(intval(preg_replace('/[^0-9]+/', '', $str), 10));
}

line break conversion from html to php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have html code loaded in array $gh[0] and i am coonverting it to plain text
if i compare with same text loaded into new variable it doesn't works
can someone help with the issue, what should be exact value in $a to match the content in $gh[0]
i want comparsion to be working , what will be output if $gh[0] is converted into plain text especially line break
<?php
$gh[0]="Net inventory (used in)/from<br>Production Activities"
$fg=$gh[0]->plaintext;
$a="Net inventory (used in)/from Production Activities"
if($a == $fg)
{
echo "match";
}
else
{
echo "No match";
}
?>
What about just stripping all elements from the input?
$gh[0]= strip_tags($gh[0]);
If you want to know what the value (of $fg) is, why don't you check it?
There's echo, print_r and var_dump.

Encoding equals symbol to html equivalent php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I attempted encoding a '=' symbol to its html equivalent through the use of:
htmlentities("This is my test and it = this");
The result is:
<p>This is my test and it = this</p>1
Notice how the equals sign is not encoded? I know there is a HTML equivalent.
What is an alternative function I can use to encode this string?
Thanks.
I know there is a HTML equivalent
The equals sign isn't encoded for HTML, there is no reason to do so.
You might be thinking of URL-encoding, which would be %3d:
urlencode("This is my test and it = this");
// => "This+is+my+test+and+it+%3D+this"
There's no need to encode the =; it's HTML-safe. If you really want to, though: =
echo str_replace('=', '=', htmlentities("This is my test and it = this"));
Demo

How to acces the second value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was wondering if you could give two values ​​to a class and then acces to the second one by POST, something like this (part of the code):
echo "<select name='selecttsk' id='selecttsk'>";
while($w = $bd->obtener_fila($tasker, 0)){
echo "<option class='opcion1' value ='".$w[1]/$w[2]."'>".$w[0]."</option>";
}
echo "</select>";
?>
and then i need to do something like this in other file
$var = $_POST[selecttsk];
but i need $w[2]
thanks
I suppose your $_POST['selecttsk'] does have the values in the following format:
"foo/bar"
You could use "explode" in PHP to get the second part (bar), for example:
$postvar = $_POST['selecttsk'];
$vars = explode("/", $postvar);
// Then
$var = $vars[1]; // Will be the $w[2] from the form
Look into: http://php.net/explode
Beware that if $w[1] or $w[2] ever contains a "/" you might get unexpected results, you could use the limit function of explode to mitigate that issue.
However - I would generally not recommend this workflow.
Why do you need to send two variables with one select?
(could you show us som example of what $w contains)

Categories