I have a PHP variable like this:
Example1
$data = '<p>This is paragraph.</p>
<p title="not-special">No!</p>
';
Example2
$data = '<p>This is paragraph.</p>
<p title="special">Yes!</p>
';
I want to have a php function, that checks whether there's p element with title="special", and if so, insert div element "YAP!".
How can this be done?
You can accomplish it in various different ways, a working one could be:
$data = '<p>This is paragraph.</p>
<p title="special">Yes!</p>
';
if (strpos($data, 'special') !== false) {
$data = '<p>This is paragraph.</p>
<p title="special">Yes!</p>
<div>YEP</div>
';
}
echo $data;
Anyway, this was not the best way to ask a question, please have a look at: How to ask a question
You could use PHP Function to check if the variable contains a certain string, see: http://php.net/manual/de/function.strpos.php
If there is a special case for formatting in a view, you can specify it easily with a variable.
<?php
// Check somewhere for the special case
$specialCase = $_GET['success'];
?>
<p>This is paragraph.</p>
<p title="special">Yes!</p>
<?php
// Display the extra div.
if ($specialCase === 'true') {
echo '<div class="yap"></div>';
}
<?php
Related
I'm trying to figure out how to check for a single class on the body tag then include a file.
My header.php contains:
</head>
<?php $class = BODY_CLASS; ?>
<body class="<?php echo $class; ?>" id="top">
And in the body:
<?php if (isset($class) && $class == 'work') { ?>
<?php include( $_SERVER['DOCUMENT_ROOT'] . MODULES . "_social.php"); ?>
<?php }; ?>
This works fine so long as I only have a single class on the body tag, but what if I have multiple tags?
for instance my body tag outputs this:
<body id="top" class="work project1">
How can I check for the work even if other classes exist?
Just change your if statement a bit and explode() your $class, by a space to then search in the array for the value with in_array(), e.g.
if (in_array("work", explode(" ", $class))) {
You can use different approaches, for example, if you are sure that body class is always the first word, you can try this
$class=current(explode(" ",BODY_CLASS)); //class that you should check
if($class=='')
or
switch($class) { }
which splits the BODY_CLASS string and gets the first value.
Anyway, you can also try searching into an array, like this
if(in_array('classname',explode(" ",BODY_CLASS)))
You could use explode together with in_array:
if (isset($class) && in_array('work', explode(' ', $class))) { ...
References:
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.in-array.php
I thought this would have been simple enough using strpos, so I must be oblivious to a horribly obvious mistake. I have this code here:
<div class="text" style="text-align:center;border:2px solid #bbb;font-size:12px;padding:0.2%;">
<?php
$root = realpath($_SERVER['DOCUMENT_ROOT']);
include "$root/scripts/display.php";
?>
</div>
Within the included file is a simple array that displays a random string from it:
$data=array("random string","something","something else","and so on");
$info=$data[rand(0,count($data)-1)];
echo $info;
What I want to do is to display an image that is relevant to a few of the random strings that are echoed. I thought I could do this with an if check for strpos as, for example "random" in $info, such that:
...
?>
</div>
<?php if(strpos($info,"random") !== false){?>
<img src=...>
<?php } ?>
Problem is, this displays the image, even if "random" was not echoed.
I have a feeling this might have something to do with $info being checked, right?
can you please check with the following if it solves you problem
$data=array("1","2","3","4","5");
$info = array_rand($data, count($data));
print_r($info);
<?php if(in_array("4", $info)){?>
<img src=...>
<?php } ?>
I have got a string contain with html tags.I want to print it using php , but it print with html tags.How to print somethings like this?
String :
<p style="color:#f00">HTML basic test Text</p>
Want to show :
HTML basic test Text
currently I show the result : <p style="color:#f00">HTML basic test Text</p>
Have you any solution?
Like CBroe said, you can use strip_tags() function
<?php
$text = '<p style="color:#f00">HTML basic test Text</p>';
echo strip_tags($text);
?>
AFAICT an echo should do it. If I need some HTML code to be generatet with php I echo it like:
<?php
echo '<p>Hallo world</p>';
?>
Worked for me, I never used print for that purpose.
I have got a Html and Javascript code, that contains about 1000 lines and I need to put it to php variable.
Sure I was thinking about the EOT method, But there is one problem with it, if there is word function like in javascript is, it will take it like php function, and this will cause errors.
Any other Idea how to do it?
I have already tried other forums, but they can't help me, so I hope they can help me on the best.
Maybe use output buffering...
<?php
ob_start();
?>
<b>
<u>
<font color="#FF0000">
<blink>
<marquee>
1000
LINES
OF
HTML
AND
JAVASCRIPT!
</marquee>
</blink>
</font>
</u>
</b>
<?php
$content = ob_get_contents();
ob_clean();
?>
Then your HTML and JavaScript will be in the $content variable.
You could read directly from an HTML file on disk, using file_get_contents().
You can use the EOF method.
There's no problem with reserved words in that case. (As far as I know)
EDIT:
$output .= <<<HTML
function bla()
{
//Something
}
HTML;
Won't be treated as a php function.
Try this;
class Temp
{
public function html($path)
{
ob_start()
require(path); // or file_get_contents(<URI>);
$html = ob_get_clean ();
return $html
}
}
$temp = new Temp();
$htmlData = $temp->html('somepath/somefile.php')
echo $htmlData;
I haven't found anytihng in Google or the PHP manual, believe it or not. I would've thought there would be a string operation for something like this, maybe there is and I'm just uber blind today...
I have a php page, and when the button gets clicked, I would like to change a string of text on that page with something else.
So I was wondering if I could set the id="" attrib of the <p> to id="something" and then in my php code do something like this:
<?php
$something = "this will replace existing text in the something paragraph...";
?>
Can somebody please point me in the right direction? As the above did not work.
Thank you :)
UPDATE
I was able to get it working using the following sample:
Place this code above the <html> tag:
<?php
$existing = "default message here";
$something = "message displayed if form filled out.";
$ne = $_REQUEST["name"];
if ($ne == null) {
$output = $existing;
} else {
$output = $something;
}
?>
And place the following where ever your message is to be displayed:
<?php echo $output ?>
As far as I can get from your very fuzzy question, usually you don't need string manipulation if you have source data - you just substitute one data with another, this way:
<?php
$existing = "existing text";
$something = "this will replace existing text in the something paragraph...";
if (empty($_GET['button'])) {
$output = $existing;
} else {
$output = $something;
}
?>
<html>
<and stuff>
<p><?php echo $output ?></p>
</html>
but why not to ask a question bringing a real example of what you need? instead of foggy explanations in terms you aren't good with?
If you want to change the content of the paragraph without reloading the page you will need to use JavaScript. Give the paragraph an id.<p id='something'>Some text here</p> and then use innerHTML to replace it's contents. document.getElementById('something').innerHTML='Some new text'.
If you are reloading the page then you can use PHP. One way would be to put a marker in the HTML and then use str_replace() to insert the new text. eg <p><!-- marker --></p> in the HTML and $html_string = str_replace('<!-- marker -->', 'New Text', $html_string) assuming $html_string contains the HTML to output.
If you are looking for string manipulation and conversion you can simply use the str_replace function in php.
Please check this: str_replace()
If you're using a form (which I'm assuming you do) just check if the variable is set (check the $_POST array) and use a conditional statement. If the condition is false then display the default text, otherwise display something else.