I have an example.php form when posted to perl script example.pl which responds either this
<!-- comments disabled -->
or this
<!-- comments enabled -->
The above response is stored in php get request in $commentstatus variable
Response may be a multiline or single line !
How to build an expression or verify the response using php preg_match or a normal validation for if else condition in php?
You can check for the presence of a string, within a string with strpos().
So the actual code would be:
<?php
if (strpos($commentstatus, "<!-- comments disabled -->") !== false)
{
}
elseif(strpos($commentstatus, "<!-- comments disabled -->") !== false)
{
}
function commentsEnabled($str){
preg_match_all('/^.*?<!--\s*comments\s+(en|dis)abled\s*-->.*$/m', $str, $m);
return !count($m[1]) ? null : array_pop($m[1]) == 'en';
}
demo
enlarge image
Credits to #lbu for the SIMPLEST solution who commented under the Op
This works excellently and is the easiest
$content= trim($commentstatus);
if ($content == "<!--commentsdisabled-->"){
print "Dear User Comments are disabled for your access";
}else if($content == "<!--commentsenabled-->"){
print "Dear User Comment added to blog post entry";
}else{
print "You are Banned for Spamming";
}
#degenerate
"Learn how to read the PHP documentation," - rude and assumptious
But your code works, even for line breaks read in from a file.
if (preg_match('#enabled#', $commentstatus)){
// Enabled code
}else{
// Everything else code
}
I don't know how it works, because the documentation you linked says to use the 's' switch :
"The s tells the code NOT TO stop searching when it encounters \n (line break)"
This works is with preg_match_all & preg_match !!
It's a plus 1 even with the ending snark!
Not sure what #BetaCoder is on about, but it's obviously not a question.
Not sure if you are simply asking this?
if (preg_match('#enabled#', $commentstatus)){
// Enabled code
}else{
// Everything else code
}
Learn how to read the PHP documentation, please:
http://us1.php.net/preg_match
Related
I am building an admin panel that can be used on different sites. I need to show some specific HTML if the url contains a specific domain, and something else if not.
So, if the URL is something like this http:// (something) .mydomain.com/admin.... I would like to show some HTML.
If the URL is something like this http:// anotherdomain.com/admin... I would like to show something else.
Í have tried with this, but somehow didn't get that to work. What am I doing wrong?
Edit: Wrong code. See code below...
<?php
if (substr($_SERVER['REQUEST_URI'], 0, 5) !== '/mydomain.com') {
echo '<div id="stuff"></div>';
}
?>
Sorry: Posted the wrong php code. A bit too tired I think...
This is the one I messed with:
<?php
if (substr($_SERVER['SERVER_NAME']) !== 'mydomain.com') {
echo '<div id="stuff">test</div>';
}
?>
Please try below code
<?php
$url_segments = parse_url("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
if (strpos($url_segments['host'],'mydomain.com') !== false) {
echo 'true';
}else{
echo 'false';
}
?>
Thanks,
Try with that code:
if (preg_match ( string $pattern , string $subject)){}
http://php.net/manual/de/function.preg-match.php
http://php.net/substr
Your current substr is saying "If the first 5 characters of the request URI aren't /mydomain.com, print the stuff below."
You could try to do this correctly in a few ways. You could use strpos and checking to see if mydomain.com appears in the request URI, or a more complicated regex to find something like (mydomain.)
Change $_SERVER['REQUEST_URI'] for $_SERVER['SERVER_NAME'] if mydomain.com it's your domain name.
Also, the substr is returning 5 chars and you are comparing against 13 chars
I'm developing a plugin for wordpress, the parameter of the $ _GET is recorded in the database according to the preference of the User via the Wordpress Admin Panel. The following validation has to be via the $ _GET, this is the function:
$db_url = get_option('my_get_url');
// returns the value of the database entered by User
// on this case return --> page=nosupport
$url_explode = explode("=", $db_url);
$url_before = $url_explode[0]; // --> page
$url_after = $url_explode[1]; // --> nosupport
echo "Before: ".$url_before; // here are ok, return --> page
echo "After: ".$url_after; // here are ok, return --> nosupport
My problem is here:
// here $_GET no have any value, dont work on validate...
if($_GET[$url_before] != ""){
if($_GET['$url_before']=="nosupport"){
// my function goes here...
}
}
I using for test the parameter:
echo $_GET[$url_before];
But dont return any value...
I found the problem, i had already tested all of these options, but ever dont working, the problem was that I was testing the function inside the main page of my site, and on the main page (mysite.com) does not get the parameter (?page=nossuport), so always returning null values, when I used the variable in the GET or used the echo $GET[$my_var] to test.. It was a great carelessness of mine, would never work...
by the way, the two parameters works correctly:
$_GET[$url_before]
$_GET["$url_before"]
The Problem are solved, Thanks for help.
if($_GET[$url_before] != ""){
if($_GET[$url_before]=="nosupport"){ // note no "" here
// my function goes here...
}
}
In your solution, the key was treated as a string, with no variables evaluated.
You forgot to take out the ' in the second condition.
You wrote:
$_GET['$url_before']
I'm guessing it should be:
$_GET[$url_before]
foreach($_GET as $key => $value){
if($key == "nosupport"){
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Im trying to build a single page that gets content using "file_get_contents" function, I have managed this part. The content ends up in a variable called $content. The idea is to get an alert (by sound) when the content changes.
What I have faild at is the part where I compare the old value for $content with the new one. Somehow I must keep the old value and compare it with the new one and when I reload the page I would like to get an alert if $content has changed. I was thinking I should use sessions for this comparison.
This is what I got:
<?php
session_start();
$_SESSION['red'] = $red;
if($red != $content) {echo "ALERT" . $content = $_SESSION['red'];}
if($red == $content) {echo "Same";}
It is not working, probably looks like crap since I have no clue what im doing really :)
Below is the code for setting the $content variable:
$page = file_get_contents('data.htm');
if ( preg_match ( '/<a tabindex="50" class="item_link".*?a>/s', $page, $matches ) )
{
foreach ( $matches as $key => $content )
{
echo $key . ' => ' . $content . '<br /><br />';
}
}
else
{
echo 'No match';
}
?>
For the Alert sound I found a script;
<script>
var audio = document.createElement('audio');
document.body.appendChild(audio);
audio.src = 'http://rpg.hamsterrepublic.com/wiki-images/2/21/Collision8-Bit.ogg';
setInterval(function(){audio.play();}, 5000);
</script>
Need help activating this script if the $content has changed, also to make it only play once.
I commend your self-efacing attitude.:)
One problem, I think, is the way you've written your if statement(s):
if($red != $content) {echo "ALERT" . $content = $_SESSION['red'];}
if($red == $content) {echo "Same";}
Firstly, the '.' after echo "ALERT" should, I think be a ';'.
Secondly, use an else rather than the 2 if statements. Why write more code than you need to and (more importantly) the assignment in your first if makes your second if true!!
Also, I think you want to be making $red = $_SESSION['red'] at the start, then make $_SESSION['red'] = $content.
Finally, use strcmp() instead of == or != (strcmp() is more reliable for comparing strings - remember it returns false when the strings match though).
So, you end up with:
<?php
session_start();
$red=(array_key_exists('red',$_SESSION)?$_SESSION['red']:'');
... set up $content
if(strcmp("$red","$content") )
{ // what you've previously read doesn't match the new content
echo "ALERT";
// escape back to the html to do the sound stuff
?>
<script>
var audio = document.createElement('audio');
document.body.appendChild(audio);
audio.src = 'http://rpg.hamsterrepublic.com/wiki-images/2/21/Collision8-Bit.ogg';
setInterval(function(){audio.play();}, 5000);
</script>
<?php
$_SESSION['red'] = $content;
// so now they both match the new content
// - so will give "same" next time round
} else {
echo "Same";
}
...
Try this out.
First, you must consider that to re-run your PHP code, your page needs to refresh.
To achieve this, you may use the classic html refresh through meta tag (I suppose you want just get things done, otherwise you need some tutorials to understand better what you are doing).
Second, you can store the content of data.htm in a file on the server (it's not clever how data.htm is supposed to change, I imagine through FTP or something similar, if it should change through browser, notice that it's a completely different story and you have to follow some tutorials).
After you have done this, you can just compare file_get_contents result with the stored file and check if they are not the same. If they are not, print out your javascript code to have your page ring and replace the content of the file with the new fetched contents (otherwise will keep ringing).
No sessions in this way.
This seems like it should be easy but I can't for the life of me get this to work, I'm checking a variable in a query string, if it returns a Yes it redirects to a new page. If it returns a no I want it to display an error message above the form.
My issue is with displaying the error message, it redirects when var=Yes but when it gives me var=no I can't get it to display the error message. If I replace $errormsg with header(..); it works so I know it's not my if statement.
if (isset($_GET['var'])) {
$var = $_GET['var'];
$errormsg = '';
if ($var == 'Yes') {
header( 'Location: http://www.google.com' ); exit;
}
else if ($var == 'no') {
$errormsg = 'This is an error message.';
}
};
And this is the error message above the form:
<div class="errormsg"><?php echo $errormsg; ?></div>
My PHP ability is limited and I can't figure out what I'm doing wrong, any help will be appreciated.
Thanks!
My guess is the $var is not returning what you are expecting (possibly a "No" instead of a "no" as mjayt suggested in the comments.
First step would be to debug, and determine what $var is actually returning. Try simple print statements and go from there.
Try to echo $errormsg directly under the if block without any HTML. See if your text is returned.
Think you might remove that semi colon from that last curly brace also
I'm trying to display a custom welcome message to visitors based off what social media site they are coming in from. I can't get this string to work but it is echoing back no matter what the refering site is. Can anyone help me get this to work? Many thanks!
<?php
if (strpos("twitter.com",$_SERVER[HTTP_REFERER])==0) {
echo "Welcome, Twitter User! If you enjoy this post, don't hesitate to retweet it to your followers";
}
?>
Tried using
<?php
if(isset($_SERVER['HTTP_REFERER'])) {
echo $_SERVER['HTTP_REFERER'];
}
?>
to get this to work but no luck.
strpos returns false when the search word does not appear in the string. 0 also evaluates to false when compared to a boolean. So false == 0, and your code always runs.
Use a strict comparison to require both value and type match instead of ==
if (strpos("twitter.com", $_SERVER['HTTP_REFERER']) === 0) {
echo "Welcome, Twitter User! If you enjoy this post, don't hesitate to retweet it to your followers";
}
However, the referrer will not start with twitter.com, it'll start with http:// or https:// so your condition wasn't right in the first place. To search for twitter.com anywhere else in the string:
if (strpos("twitter.com", $_SERVER['HTTP_REFERER']) !== false) {
echo "Welcome, Twitter User! If you enjoy this post, don't hesitate to retweet it to your followers";
}
I think you may have confused strpos() with strcmp(), being strcmp() returns 0 when two strings are equal. strpos() returns the position at which a string was found. Try:
<?php
if (strpos('twitter.com', $_SERVER['HTTP_REFERER'] != 0)) {
echo "Welcome, twitter user.";
}
?>