GET_CONTENT name from txt file - php

i have this list on name.txt file :
"name1":"Robert"
"name2":"George"
"name3":"Flophin"
"name4":"Fred"
in a web page i need a php code that takes only the name of the person by the name 1 2 3 4 id.
I've use this in test.php?id=name2
$Text=file_get_contents("./name.txt");
if(isset($_GET["id"])){
$id = $_GET["id"];
$regex = "/".$id."=\'([^\']+)\'/";
preg_match_all($regex,$Text,$Match);
$fid=$Match[1][0];
echo $fid;
} else {
echo "";
}
The result should be George ,
how do i change this to work??
Mabe is another way to do this more simply?

$file=file('name.txt');
$id = $_GET["id"];
$result=explode(':',$file[$id-1]);
echo $result[1];
Edit: $result[1] if you want just name.

Heres an ugly solution to your problem, which you can loop trough.
And Here's a reference to the explode function.
<?php
$text = '"name1":"Robert"
"name2":"George"
"name3":"Flophin"
"name4":"Fred"';
$x = explode("\n", $text);
$x = explode(':', $x[1]);
echo $x[1];

Load the text file into an array; see Text Files and Arrays in PHP as an example.
Once the array is loaded you can reference the array value directly, e.g. $fid = $myArray['name' . $id]. Please refer to PHP how to get value from array if key is in a variable as an example.

Related

How to change place of text in echo?

I'm setting up a script for a friend, I want the landing_{random} to be added after the domain names but I whatever I do it shows behind the URL. How can I fix this issue?
I have tried moving the variables around and reversing places with the variables.
<?php
$lines = file('domains.txt');
foreach($lines as $line) {
$randomString = substr(str_shuffle("0123456789abcdef"), 0, 1) .
substr(str_shuffle("0123456789abcdef"), 0, 6);
$landing = "/landing_$randomString";
/*echo "$line.'/landing_'.$randomString";*/
echo "$line{$landing}";
}
?>
I want when I input URL in http://example.com in domains.txt for it to output http://example.com/landing_d2ae5b3
It will not work like that Try instead:
$landing = '/landing_".$randomString."';
Should work fine now
Update answer
Try this
$landing = "/landing_".$randomString."";
By the way I think you may need to define array before use it like
$lines = explode("" ,$lines);
//then
$landing = "/landing_".$randomString."";
Also you may use this :
echo "$line".$landing."";

Update array in a file

I have an include with a single array in it that holds 3 instructions; a "y/n" switch and a start and end date. The include meetingParams.php looks like this:
<?php
$regArray = array("n","2018-03-03","2018-03-07");
?>
I want to update those array values from time to time using a web based form. Where I get stuck is finding the correct syntax to do that. Right now I have the following:
$registration = $_POST['registration'];
$startMeeting = $_POST['startMeeting'];
$endMeeting = $_POST['endMeeting'];
$replace = array($registration, $startMeeting, $endMeeting);
$search = file_get_contents('includes/meetingParams.php');
$parsed = preg_replace('^$regArray.*$', $replace, $search);
file_put_contents("includes/meetingParams.php", $parsed);
When I run this code, the file meetingParams.php get's replaced with an empty file. What am I missing?
This should work fine:
$content = '<?php
$regArray = array("'.$registration.'","'.$startMeeting.'","'.$endMeeting.'");
?>';
file_put_contents("includes/meetingParams.php", $content);
Try this.
include_once "includes/meetingParams.php";
$registration = $_POST['registration'];
$startMeeting = $_POST['startMeeting'];
$endMeeting = $_POST['endMeeting'];
$regArray = array($registration, $startMeeting, $endMeeting);
Explanation
There is no need to use file_get_contents since you are using a PHP file you can simply include it.
What that means is that you are placing that file inside your script. Then there is no need to use RegEx to replace the array, just reassign its value.

How to get values separated with comma from database using PHP

I have four files named comma separated in one field in database like this file1,file2,file3,file4. It may change depending on files uploading. User can upload maximum 4 files, minimum one file. But I was not able to get it. I used explode but it's taking too long.
I am using this code:
$imagefiles = $row["imagefiles"];
$cutjobs = explode(",", $imagefiles);
$cutjobs1 = count($cutjobs);
$image1 = $cutjobs[0];
$image2 = $cutjobs[1];
$image3 = $cutjobs[2];
$image4 = $cutjobs[3];
if (empty($image1)) {
$imagefiles1 = "";
} else {
$imagefiles1 = 'http://projects.santabantathegreat.com/glassicam/uploads/'.$registerid.
"/".$viewjobsid.
"/".$image1;
}
if (empty($image2)) {
$imagefiles2 = "";
} else {
$imagefiles2 = 'http://projects.santabantathegreat.com/glassicam/uploads/'.$registerid.
"/".$viewjobsid.
"/".$image2;
}
if (empty($image3)) {
$imagefiles3 = "";
} else {
$imagefiles3 = 'http://projects.santabantathegreat.com/glassicam/uploads/'.$registerid.
"/".$viewjobsid.
"/".$image3;
}
if (empty($image4)) {
$imagefiles4 = "";
} else {
$imagefiles4 = 'http://projects.santabantathegreat.com/glassicam/uploads/'.$registerid.
"/".$viewjobsid.
"/".$image4;
}
}
$data[] = array( 'imagearray' => array($imagefiles, $imagefiles1, $imagefiles2, $imagefiles3));
}
echo json_encode($data);
}
I am getting output like this :
[{"imagearray":["http:\/\/projects.santabantathegreat.com\/glassicam\/uploads\/60\/30\/file1.jpg","http:\/\/projects.santabantathegreat.com\/glassicam\/uploads\/60\/30\/file2.jpg",""]}]
If you see this imageArray last one is getting "" that means some in file1, file2, file3, file4 one name is missing so I want to show if any filename is not there means I don't want to show null values with ""
i have a field with file1,file2,file3,file4 so times we will have file1,file3 then remaining will not there so i want to count file name separated with commas and if file1 is there is should print that if file3 is there not then it shouldn't show with ""
You could have used split(), but its deprecated in PHP 5.3.0. So, instead you are left with:
explode() which is substantially faster because it doesn't split based on a regular expression, so the string doesn't have to be analyzed by the regex parser.
or
preg_split() which is faster and uses PCRE regular expressions for regex splits.
With preg_split() you could do:
<?php
$encoded_data = json_encode($data);
$images = preg_split('/,/', $encoded_data->imagearray);
?>
I would say that explode() is more appropriate for this.
<?php
$encoded_data = json_encode($data);
$images = explode(',', $encoded_data->imagearray);
print_r($images);
?>
Resources: What is the difference between split() and explode()?
You shouldn't have empty values in your array in the first place. But if you still have any empty values you could use preg_split() something like this one here.
Similarly you can use array_filter() to handle removal of values (null, false,'',0):
print_r(array_filter($images));
There are so many answers here in this forum that do exactly what you are asking: Remove empty array elements, Delete empty value element in array.

How can I sanitise the explode() function to extract only the marker I require?

I have some php code that extracts a web address. The object I have extracted is of the form:
WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults
Now in PHP I have called this object $linkHREF
I want to extract the id element only and put it into an array (I'm bootstrapping this process to get multiple id's)
So the command is:
$detailPagePathArray = explode("id=",$linkHREF); #Array
Now the problem is the output of this includes what comes after the id tag, so the output looks like:
echo $detailPagePathArray[0] = WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&w
echo $detailPagePathArray[1] = bf&page=1&
echo $detailPagePathArray[2] = 16123012&source=searchresults
Now the problem is obvious, where it'd firstly picking up the "id" in the "wid" marker and cutting it there, however the secondary problem is it's also picking up all the material after the actual "id". I'm just interested in picking up "16123012".
Can you please explain how I can modify my explode command to point it to the particular marker I'm interested in?
Thanks.
Use the built-in functions provided for the purpose.
For example:
<?php
$url = 'http://www.example.com?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults';
$qs = parse_url($url);
parse_str($qs['query'], $vars);
$id = $vars['id'];
echo $id; // 16123012
?>
References:
parse_url()
parse_str()
if you are sure that you are getting &id=123456 only once in your object, then below
$linkHREF = "WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults";
$str = current(explode('&',end(explode('&id', $linkHREF,2))));
echo "id" .$str; //output id = 16123012

Get some value from url?

I have this kind of url from youtube, with the value of video
$url='http://www.youtube.com/watch?v=H_IkPia6eBA&';
What i need is just to get new string with value of V etc in this case
$newstring='H_IkPia6eBA&';
I dont know how long V could be, only i need to get that value of V, I have tried
$string = 'http://www.youtube.com/watch?v=oYyslNuRcwM';
$url = parse_url($string);
parse_str($url['query'], $query);
print_r($query);
Tried with this, but in CodeIgniter post, I only get empty array?
You're almost there already.
<?php
$string = 'http://www.youtube.com/watch?v=oYyslNuRcwM';
$url = parse_url($string);
parse_str($url['query'], $query);
$newstring=$query["v"]; // just this line is missing
echo $newstring;
?>
Demo
But you know something? If the url format is always going to be like that then no need for all those functions. It then can simply be
<?php
$url='http://www.youtube.com/watch?v=H_IkPia6eBA&';
echo str_replace("http://www.youtube.com/watch?v=","",$url);
?>

Categories