I have this code:
<?php $url = JURI::getInstance()->toString();
if ($url == "http://example.com/news/latest/"){
echo "This is latest page";
} else {
echo "This is not latest page";
}
?>
What I'm trying to do is instead of 'http://example.com/news/latest/', how can I select the pages/items under /latest/. If it makes any more sense, here's a syntax:
if ($url == "http://example.com/news/latest/" + ANYTHING UNDER THIS)
I cannot use not equal to ($url !=) since it will include other parent pages not equal to /latest/. I just want what's under it. If anyone understands it, I need help on how to put it into code.
Update:
What I'm trying to do is if the page is example.com/news/latest, it will echo "Latest". And if for example, I am in example.com/news/latest/subpage1/subpage2, it will echo "You are in a page that is under Latest." Anything beyond "Latest" will echo that.
$str = 'example.com/news/latest/dfg';
preg_match('/example.com\/news\/([^\/]+)\/?(.*)/', $str, $page);
if(isset($page[2]) && $page[2])
echo 'You are under: ' , $page[1];
elseif(isset($page[1]))
echo 'At: ' , $page[1];
else
echo 'Error';
Edit: after clarification switched to regular expression.
Use a regular expression:
$matches = array();
if((preg_match('#http://example\.com/news/latest/(.*)#', $url, $matches)) === 1) {
if(strlen($matches[0]) > 0) {
echo "You're at page: $matches[0]";
} else {
echo "You're at the root";
}
} else {
// Error, incorrect URL (should not happen)
}
EDIT: Fixed, untested so you may have to tweak it a little
Related
The problem is with this line:
if $var LIKE '1800%';
and I'm not sure how to fix it. Thanks.
<?php
//check to see if account number is like 1800*
if (isset($_POST['acct_number'])) {
$var = $_POST['acct_number'];
if $var LIKE '1800%'; {
//stop the code
exit;
} else {
echo 'normal account number';
}
}
?>
You need PHP not MySQL. For 1800% just check that it is found at position 0:
if(strpos($var, '1800') === 0) {
//stop the code
exit;
} else {
echo 'normal account number';
}
If it can occur anywhere like %1800% then:
if(strpos($var, '1800') !== false) {
//stop the code
exit;
} else {
echo 'normal account number';
}
Use substr function to get first 4 characters and compare it with 1800.
if(substr($var, 0, 4) == '1800')
{
// your code goes here.
}
``
Another way could be to use strpos()
if (strpos($var, '1800') === 0) {
// var starts with '1800'
}
I would use a regular expression for this preg_match('/^1800.+/', $search, $matches);
I need help.
As I do?
String same other string with different number.
Example
if ($current_server == "Lobby-01") {
echo "visiting in Lobby"
} elif ($current_server == "Lobby-02"){
echo "visiting in Lobby"
} and more..
I thought about trying
if ($current_server == "Lobby-/[0-99]+/"){
echo "visiting in Lobby"
} else {
//is false, then it will not show the message
}
is correct?
How do I do it?
Can you help me?
Use preg_match:
if (preg_match("/\bLobby-[0-9]{2}\b/i", $current_server)) {
echo "visiting in Lobby";
}
else {
// no message
}
Demo
So, i've been working on making a search engine without the use of a database. What it's supposed to do is find the word searched for in the webpage and automatically give it's link. Here's it's code:
<?php
session_start();
$searchInput = $_POST['search'];
$inputPage1 = $_SESSION['pOneText'];
$inputPage2 = isset($_SESSION['pTwoText']) ? $_SESSION['pTwoText'] : "";
$inputPage3 = isset($_SESSION['pThreeText']) ? $_SESSION['pThreeText'] : "";
$fUrl = file_get_contents("mDummyP.php");
$sUrl = file_get_contents("sDummyP.php");
$tUrl = file_get_contents("tDummyP.php");
if (substr_count($fUrl, $searchInput) !== false) {
echo "All results for <strong> $searchInput </strong> : <br>" ;
} elseif (substr_count($sUrl, $searchInput) !== false) {
echo "All results for <strong> $searchInput </strong> : <br>";
} elseif (substr_count($tUrl, $searchInput) !== false) {
echo "All results for <strong> $searchInput </strong> : <br>";
} else {
echo "No resulst for <strong> $searchInput </strong>! ";
}
?>
However, it never checks if the word actually exists or not, it always returns "all results for". So, i was wondering if anyone knew why or had suggestions to improve it. Keep in mind that it will never be used professionally, it's just to test my abilities. Thanks in advance!
You need to look at the php manual for substr_count
Return Values
This function returns an integer.
Thus it will ALWAYS go into this first if block:
if (substr_count($fUrl, $searchInput) !== false) {
because the return value of substr_count is only ever an integer and is NEVER going to be false - your code is checking for the exact value and type of false.
Furthermore, apart from the else block, all of the statements are just echo'ing the exact same string so you will not see any differentiation of output if execution did go into the if or elseif blocks. See below to get you on the right track:
$searchInput = 'some';
$fUrl = 'this is test file text';
$sUrl = 'this is some other text';
$tUrl = 'extra text';
if (substr_count($fUrl, $searchInput) !== 0) {
echo "a";
} elseif (substr_count($sUrl, $searchInput) !== 0) {
echo "b";
} elseif (substr_count($tUrl, $searchInput) !== 0) {
echo "c";
} else {
echo "No resulst for <strong> $searchInput </strong>! ";
}
Now I want to check if this text box contains one word or two, for example
if ($_POST['mytext'] == two words){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
and I tried
if ($_POST['mytext'] > 1){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
and it didn't work
that what I mean so how to make it?
Hope to find a way to do that.
Thanks
If you define two words as "some characters followed by one space followed by some characters" then you can do something like:
$mytext = $_POST["mytext"];
$parts = explode(" ", $mytext);
if (count($parts) !== 2) {
throw new Exception("too many or too little!");
}
if (strlen($parts[0]) === 0 || strlen($parts[1]) === 0) {
throw new Exception("not enough characters!");
}
Keep in mind that this allows a string like "# !"
Use str_word_count():
if (str_word_count($_POST['mytext']) > 1){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
you could use the
`substr_count('some text', ' ');
it will return the number of space,.
try this
$text= preg_split(" ",$_POST['mytext']);
if (count($text) > 1){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
I would like to know if some word is present in the URL.
For example, if word car is in the URL, like www.domain.com/car/ or www.domain.com/car/audi/ it would echo 'car is exist' and if there's nothing it would echo 'no cars'.
Try something like this. The first row builds your URL and the rest check if it contains the word "car".
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'car') !== false) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
I think the easiest way is:
if (strpos($_SERVER['REQUEST_URI'], "car") !== false){
// car found
}
$url = " www.domain.com/car/audi/";
if (strpos($url, "car")!==false){
echo "Car here";
}
else {
echo "No car here :(";
}
See strpos manual
if( strpos( $url, $word ) !== false ) {
// Do something
}
worked for me with php
if(strpos($_SERVER['REQUEST_URI'], 'shop.php') !== false){
echo 'url contains shop';
}
This worked for me:
// Check if URL contains the word "car" or "CAR"
if (stripos($_SERVER['REQUEST_URI'], 'car' )!==false){
echo "Car here";
} else {
echo "No car here";
}
If you want to use HTML in the echo, be sure to use ' ' instead of " ".
I use this code to show an alert on my webpage https://geaskb.nl/
where the URL contains the word "Omnik"
but hide the alert on pages that do not contain the word "Omnik" in the URL.
Explanation stripos : https://www.php.net/manual/en/function.stripos
strstr didn't exist back then?
if(strstr($_SERVER['REQUEST_URI'], "car")) {
echo "car found";
}
This must be one of the easiest methods right?
Have a look at the strpos function:
if(false !== strpos($url,'car')) {
echo 'Car exists!';
}
else {
echo 'No cars.';
}
Surely this is the correct way round....
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (!strpos($url,'mysql')) {
echo 'No mysql.'; //swapped with other echo statement
} else {
echo 'Mysql exists.';
}
Otherwise its reporting the opposite way it should...
Starting with PHP 8 (2020-11-24), you can use str_contains:
if (str_contains('www.domain.com/car/', 'car')) {
echo 'car is exist';
} else {
echo 'no cars';
}
You can try an .htaccess method similar to the concept of how wordpress works.
Reference: http://monkeytooth.net/2010/12/htaccess-php-how-to-wordpress-slugs/
But I'm not sure if thats what your looking for exactly per say..
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (!strpos($url,'car')) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
This seems to work.