Good day.
I have link http://vk.com/id98429809?z=photo98429809_299166823%2Falbum98429809_0%2Frev
Tell me please how get:
$id=$arr['id']; // $id = 98429809
$type=$arr['type']; // $type= photo
$num=$arr['num']; // $num= 299166823
Tell me please how explode link and get this ?
Try this,
<?php
$str='photo98429809_299166823';
$arr=explode('_',$str);
$num=$arr[1]; // $num= 299166823
preg_match('/^[a-z]+/i', $arr[0], $m);
$type=$m[0];
$id=str_replace($type,"",$arr[0]);
$num=$arr[1];
echo '<br/>';
echo $id;
echo '<br/>';
echo $type;
echo '<br/>';
echo $num;
?>
Tested on http://writecodeonline.com/php/
Try it, i think it's easy and useful for u
$str=$_GET["z"];
$arr['type']=substr($str,0,5) ;
$arr['id']=substr($str,5,8) ;
$arr['num']=substr($str,14,3) ;
echo $arr['id']." ".$arr['type']." ".$arr['num'];
$url = "http://vk.com/id98429809?z=photo98429809_299166823%2Falbum98429809_0%2Frev";
if (preg_match('#=([^\d]+)([\d]+)_([\d]+)#', $url, $matches)) {
$id = $matches[2];
$type = $matches[1];
$num = $matches[3];
}
Universal answer for all links vk.com
if(substr_count($link,"photo")>0){
$type='photo'; $arg='photo';
}
elseif(substr_count($link,"video")>0){
$type='video'; $arg='video';
}
elseif(substr_count($link,"id")>0){
$type='id'; $arg='id';
}
else{
$type='id'; $arg='/';
}
$arr1=explode($arg,$link);
if($arg=='photo'){
$arr2=explode($arg,$arr1[1]);
$arr3=explode('_',$arr2[0]);
$id=$arr3[0];
$item_id=substr($arr3[1],0,9);
}
elseif($arg=='video'){
$arr2=explode($arg,$arr1[1]);
$arr3=explode('_',$arr2[0]);
$id=$arr3[0];
$item_id=substr($arr3[1],0,9);
}
elseif($arg=='id' || $arg=='/'){
$id=$arr1[1];
$item_id=$arr1[1];
}
echo $link;
echo '<br>id = '.$id.'<br>item_id = '.$item_id;
Code big, but he work for all links
Thanks all for help!
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.
Can you please help me in below code.
In the below code, in_array is not working.
$d = "23232,54454,656565";
$data = explode(",", $d);
$pass = (isset($test['pass'][1]) ? $test['pass'][1] : '');
if(in_array($pass, $data)) {
echo "exist";
} else {
echo "Not Exist";
}
Thanks
i tested your code and put following line to top of that and it work:
$test['pass'][1] = '23232';
$test['pass'][1] is empty and you see "Not Exist" message
How to optimize PHP??
I'd like to optimize this code with if-statements in PHP.
$head = make_number($_GET['id']);
if(isset($head)){
echo $head;
}
else {
$head = make_number($_GET['id']);
if(isset($head)){
echo $head;
}
else {
$head = make_number($_GET['url']);
if(isset($head)){
echo $head;
}
}
}
You can use null coalescing operator from new PHP features:
$head = make_number($_GET['id']) ??
make_number($_GET['url']) ??
make_number($_GET['xyz']);
Or if you can't use php 7 features - use short-circuit OR's:
$head = make_number($_GET['id']) or
$head = make_number($_GET['url']) or
$head = make_number($_GET['xyz']);
Try this
$head1 = make_number($_GET['id']);
$head2 = make_number($_GET['url']);
if(isset($head1) && isset($head2)){
echo $head;
}
do this
$headID = make_number($_GET['id']);
$headURL = make_number($_GET['url']);
if(isset($headID))
echo $headID;
else {
if(isset($headURL))
echo $headURL;
}
Here are my 2 cents:
if(isset($_GET['id'])){
echo make_number($_GET['id']);
}
elseif(isset($_GET['url'])){
echo make_number($_GET['url']);
}
// if none of them is set
else{
echo 'Nothing to echo';
}
Hope it helps
So I've been trying to devise a function that will echo a session variable only if it is set, so that it wont create the 'Notice' about an undefined variable. I am aware that one could use:
if(isset($_SESSION['i'])){ echo $_SESSION['i'];}
But it starts to get a bit messy when there are loads (As you may have guessed, it's for bringing data back into a form ... For whatever reason). Some of my values are also only required to be echoed back if it equals something, echo something else which makes it even more messy:
if(isset($_SESSION['i'])){if($_SESSION['i']=='value'){ echo 'Something';}}
So to try and be lazy, and tidy things up, I have tried making these functions:
function ifsetecho($variable) {
if(!empty($variable)) {
echo $variable;
}
}
function ifseteqecho($variable,$eq,$output) {
if(isset($variable)) {
if($variable==$eq) {
echo $output;
}
}
}
Which wont work, because for it to go through the function, the variable has to be declared ...
Has anyone found a way to make something similar to this work?
maybe you can achieve this with a foreach?
foreach ($_SESSION as $variable)
{function ifseteqecho($variable,$eq,$output) {
if($variable==$eq) {
echo $output;
}
else echo $variable;
}
}
now this will all check for the same $eq, but with an array of corresponding $eq to $variables:
$equiv = array
('1'=>'foo',
'blue'=>'bar',);
you can check them all:
foreach ($_SESSION as $variable)
{function ifseteqecho($variable,$equiv) {
if(isset($equiv[$variable])) {
echo $equiv[$variable];
}
else {
echo $variable;
}
}
}
Something like this?, you could extend it to fit your precise needs...
function echoIfSet($varName, array $fromArray=null){
if(isset($fromArray)){
if(isset($fromArray[$varName])&&!empty($fromArray[$varName])){
echo $fromArray[$varName];
}
}elseif(isset($$varName)&&!empty($$varName)){
echo $$varName;
}
}
You may use variable variables:
$cat = "beautiful";
$dog = "lovely";
function ifsetecho($variable) {
global $$variable;
if(!empty($$variable)){
echo $$variable;
}
}
ifsetecho("cat");
echo "<br/>";
ifsetecho("dog");
echo "<br/>";
ifsetecho("elephant");
UPDATE: With a rather complex code I’ve managed to meet your requirements:
session_start();
$cat = "beautiful";
$dog = "lovely";
$_SESSION['person']['fname'] = "Irene";
function ifsetecho($variable){
$pattern = "/([_a-zA-Z][_a-zA-Z0-9]+)".str_repeat("(?:\\['([_a-zA-Z0-9]+)'\\])?", 6)."/";
if(preg_match($pattern, $variable, $matches)){
global ${$matches[1]};
if(empty(${$matches[1]})){
return false;
}
$plush = ${$matches[1]};
for($i = 2; $i < sizeof($matches); $i++){
if(empty($plush[$matches[$i]])){
return false;
}
$plush = $plush[$matches[$i]];
}
echo $plush;
return true;
}
return false;
}
ifsetecho("cat");
echo "<br/>";
ifsetecho("dog");
echo "<br/>";
ifsetecho("elephant");
echo "<br/>";
ifsetecho("_SESSION['person']['fname']");
echo "<br/>";
ifsetecho("_SESSION['person']['uname']");
echo "<br/>";
I am not able to figure out why my condition is not working while the ip address is in the array. Why condition is failing as shown in image
<?php $valid_ip_list = explode(',',$this->valid_ips);
echo $client_ip = $_SERVER['REMOTE_ADDR'];
print('<pre>');
print_r($valid_ip_list);
if(in_array($client_ip ,$valid_ip_list))
{
echo 'I am here';
}
else
{
echo 'Condition fail';
}
?>
Problem solved with the help of array_map('trim', explode(',', $valid_ips))
This should help
$valid_ips = '192.100.100.61,192.100.100.2,127.0.0.1';
// authorized
if (in_array($_SERVER['REMOTE_ADDR'], array_map("trim", explode(',', $valid_ips)))) {
//...
}
// unauthorized
else {
//...
}