I have something like this :
<?php
$siteurl = "http://www.example.com/";
$pageid = "ed2689";
$pageurl = $siteurl.$pageid;
?>
and the links will be like this:
http://www.example.com/ed2689
http://www.example.com/report/ed2689
I have used preg_match to check the format for each one of this links
for the first link, it must be exactly like this:
$siteurl.[a-z0-9]
and i have used this :
if (preg_match('/[$siteurl]+[a-z0-9]/', $pageurl) && !preg_match('/[=]|[?]/', $pageurl))
{
echo 'Ok',
}
and for for the second link, it must be exactly like this:
$siteurl.'report/'.[a-z0-9]
and i have used this :
if (preg_match('/[$siteurl]+[report]+[a-z0-9]/', $req_uri) && !preg_match('/[=]|[?]/', $req_uri))
{
echo 'Ok',
}
and it doesn't work correctly..any help please ?
Thanks.
Let me know if not works
<?php
$siteurl = 'http://www.example.com/report/ed2689';
//$siteurl = 'http://www.example.com/ed2689';
if(strpos($siteurl,'www.example.com') === false) echo 'not my site';
else {
$arr = explode('www.example.com/',$siteurl);
$suburl = trim($arr[1]);
if (preg_match('/^[a-z0-9]+$/', $suburl) || preg_match('/^report\/[a-z0-9]+$/', $suburl))
{
echo 'ok';
}
}
?>
Related
Is there a way to code this function/executable..
if($img1 > "")
{
$show = $img1;
}
elseif($img2 > "")
{
$show = $img2;
}
elseif($img3 > "")
{
$show = $img3;
}
else
{
$show = 'default.jpg';
}
echo $show;
..in a more simple way? Thx.
Any time you end up using variables named 1,2,3,etc it means you should be using an array.
If you had it like this, for example:
$img = [
1=> '',
2=> '',
3=> 'image.jpg'
];
You could then process this like so:
// Remove empty values
$img = array_filter($img);
// Echo the first value found (image.jpg)
echo current($img);
Or to add your default:
echo (count($img)) ? current($img) : 'default.jpg';
This uses a ternary operator to echo 'default.jpg' if count($img) is 0.
If you know the id number of the image, you could do something like this:
<?php
$images = array(
'myimg.jpg',
'my_awesome_img.jpg',
'awesome_awesome.jpg'
);
$show = $images[$x];
?>
You can just do
if(!empty($img1)) {
echo $img1;
} elseif(!empty($img2)) {
echo $img2;
} elseif(!empty($img3)) {
echo $img3;
} else {
echo 'default.jpg';
}
given that you only ever want to display one of these.
Did not test, but something like this:
<?php
$img1 = 'hello';
$img2 = 'world';
$array = [1,2,3];
for($i=1; $i<count($array); $i++){
$v = ${"img{$i}"};
if( !empty($v) ){
$show = $v;
break;
}
}
echo $show;
No idea what you're trying to accomplish or why the code looks like that, seems poorly constructed from the get-go.
I'm not so sure whether it is smart to post both problems in one question, but lets try:
So, I was checking my server's error log and it still has two notices, both about "Array to string conversion in [...]".
The first line should be this:
$replace = $route['keywords'][$key]['prepend'].$params[$key].$route['keywords'][$key]['append'];
Context:
// Build an url which match a route
if ($this->use_routes || $force_routes) {
$url = $route['rule'];
$add_param = array();
foreach ($params as $key => $value) {
if (!isset($route['keywords'][$key])) {
if (!isset($this->default_routes[$route_id]['keywords'][$key])) {
$add_param[$key] = $value;
}
} else {
if ($params[$key]) {
$replace = $route['keywords'][$key]['prepend'].$params[$key].$route['keywords'][$key]['append'];
} else {
$replace = '';
}
$url = preg_replace('#\{([^{}]*:)?'.$key.'(:[^{}]*)?\}#', $replace, $url);
}
}
$url = preg_replace('#\{([^{}]*:)?[a-z0-9_]+?(:[^{}]*)?\}#', '', $url);
if (count($add_param)) {
$url .= '?'.http_build_query($add_param, '', '&');
}
}
The second one is this line:
$uri_path = __PS_BASE_URI__.$id_image.($type ? '-'.$type : '').$theme.'/'.$name.'.jpg';
as part of this:
// legacy mode or default image
$theme = ((Shop::isFeatureActive() && file_exists(_PS_PROD_IMG_DIR_.$ids.($type ? '-'.$type : '').'-'.(int)Context::getContext()->shop->id_theme.'.jpg')) ? '-'.Context::getContext()->shop->id_theme : '');
if ((Configuration::get('PS_LEGACY_IMAGES')
&& (file_exists(_PS_PROD_IMG_DIR_.$ids.($type ? '-'.$type : '').$theme.'.jpg')))
|| ($not_default = strpos($ids, 'default') !== false)) {
if ($this->allow == 1 && !$not_default) {
$uri_path = __PS_BASE_URI__.$ids.($type ? '-'.$type : '').$theme.'/'.$name.'.jpg';
} else {
$uri_path = _THEME_PROD_DIR_.$ids.($type ? '-'.$type : '').$theme.'.jpg';
}
} else {
// if ids if of the form id_product-id_image, we want to extract the id_image part
$split_ids = explode('-', $ids);
$id_image = (isset($split_ids[1]) ? $split_ids[1] : $split_ids[0]);
$theme = ((Shop::isFeatureActive() && file_exists(_PS_PROD_IMG_DIR_.Image::getImgFolderStatic($id_image).$id_image.($type ? '-'.$type : '').'-'.(int)Context::getContext()->shop->id_theme.'.jpg')) ? '-'.Context::getContext()->shop->id_theme : '');
if ($this->allow == 1) {
$uri_path = __PS_BASE_URI__.$id_image.($type ? '-'.$type : '').$theme.'/'.$name.'.jpg';
} else {
$uri_path = _THEME_PROD_DIR_.Image::getImgFolderStatic($id_image).$id_image.($type ? '-'.$type : '').$theme.'.jpg';
}
}
return $this->protocol_content.Tools::getMediaServer($uri_path).$uri_path;
}
public function getMediaLink($filepath)
{
return $this->protocol_content.Tools::getMediaServer($filepath).$filepath;
}
PHP is not my strength, so I have no idea what to do :/
Also I found some other questions about Array to string notices, but it seemed to me like you can't solve them the same way...
Thanks in advance for any help!
This error is appearing because some of the variables in these two lines are supposed to be String but they are actually array.
You need to print all the variables used in these 2 lines using the var_dump() function of PHP, this will tell you which of the variables are actually an Array, but they are supposed to be a String as per your code.
On the basis of the output, you need to modify your code to fix the issue.
So, I have the following condition for wordpress site:
if (is_page(array(1111, ???)) {
//do something for page# 1111 and url that contains "names"
}
I have a page that has url as following:
example.com/names/steve
example.com/names/mike
So, I want to check if the url is "names" as the condition.
Can someone tell me how to change the condition to check for "names" in the url?
Thanks
EDIT:
I changed to the following but no luck yet.
if ((is_page(1111)) || (preg_match("/\/names$/", $_SERVER['REQUEST_URI'])))
or
if ((is_page(1111)) || (basename($_SERVER['REQUEST_URI']) == 'names'))
or
if ((is_page(1111)) || (strpos($url, 'names') !== false))
any suggestions?
With preg_match() function using regular expression:
<?php
$url = 'example.com/names/steve';
echo (preg_match("/names/i", $url)) ? 'Match' : 'No Match';
?>
so for your example try this:
if (is_page(1111) || preg_match("/\/names\//", $_SERVER['REQUEST_URI'])){
// your logic
}
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$myArray = explode("/", $link);
$page = end($myArray);
if($page == '1111') {
//Your code
}
Edit: In case URL contains trailing slash (or not)
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$actual_link = rtrim('/', $link);
$myArray = explode("/", $actual_link);
$page = end($myArray);
if($page == '1111') {
//Your code
}
my code is
<?php if($_SERVER['REQUEST_URI'] == "/scripts/script1.php")
{
echo 'yes';
}
else
{
echo 'no';
}
some time my URL come like
www.example.com/scripts/script1.php?var1=value1&var2=value2
How can do that with PHP
URL like function....
Make use of strpos in PHP
<?php
if(strpos($_SERVER['REQUEST_URI'],"/scripts/script1.php")!==false)
{
echo 'yes';
}
else
{
echo 'no';
}
A slight variation on Shankar's answer
$result = "no";
if( strpos( $_SERVER[ "REQUEST_URI" ], "/scripts/script1.php" ) !== false ) {
$result = "yes";
}
By setting the default value for the variable, you can make your code more concise and improve readability.
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