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."";
Related
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.
I need to get the content between a wordpress shortcode.
Like say [bla_blabla name="a"]Variation A[/bla_blabla]
should return Variation A
I thought of using regex to get it, but there is a shortcodes.php file in wordpress which should already be capable of doing it? Could you guide me into the correct way of doing it?
I have currently used this code
<?php
function abtest_runner($input) {
//A bit confused if you wanted equal chances of selecting one or equal changes?
//This is for equal chances.
$size = count($input);
$select = rand(0,$size-1);
$temp=array();
$selection = $input[$select];
$temp = (explode("]",$selection));
$temp = (explode("[",$temp[1]));
echo $temp[0];
}
$input[0] = '[bla_blabla name="a"]Variation A[/bla_blabla]';
$input[1] = '[bla_blabla name="b"]Variation B [/bla_blabla]';
abtest_runner($input);
?>
If you have registered you shortcode in WordPress using the [add_shortcode][1] function then, you can extract your shortcode content using the following method:
$pattern = get_shortcode_regex();
$content = '[bla_blabla name="a"]Variation A[/bla_blabla]';
$matches = array();
preg_match("/$pattern/s", $content, $matches);
print_r($matches[5]);
$matches[5] will contain your content.
Hope this helps.
So I've got a concept of how to do this - but actually implementing me is a bit of a stumper for myself; mostly due to my lack of regex experience - but let's get into it.
I'd like to 'parse' through a 'php' file that could contain something like the following:
<?php
function Something()
{
}
?>
<html>
<body>
<? Something(); ?>
</body>
</html>
<?php
// Some more code or something
?>
If interpreted exactly - the above is worthless jibberish - but it is a good example of what I'd like to be able to parse, or interpret...
The idea is that I would read the contents of the above file, and break it out into an ordered array of its respective pieces; while tracking what 'type' each 'segment' is, so that I can either simply echo it, or run an 'eval()' on it.
Effectively, I'd like to end up with an array something like this:
$FileSegments = array();
$FileSegments[0]['type'] = "PHP";
$FileSegments[0]['content'] = "
function Something()
{
}";
$FileSegments[1]['type'] = "HTML";
$FileSegments[1]['content'] = "
<html>
<body>";
$FileSegments[2]['type'] = "PHP";
$FileSegments[2]['content'] = "Something();"
And so on...
The initial idea was to simply 'include()' or 'require()' the file in question, and grab its output from the output buffer - but it dawned on me that I would like to be able to inject some 'top level' variables into each one of these files before evaluating the code. To do this, I would have to 'eval()' my injected code, with the contents of the file after said injection - but in order to do this with the ability to handle raw HTML in the file too, I would have to basically write a temporary clone of the whole file, that just had my injected code written before the actual contents... Cumbersome, and slow.
I hope you're all following here... If not I can clarify...
The only other piece I feel I should note before finalizing this question; is that I would like to retain any variables or symbols in general ( for instance the 'Something() function ) created in segments 0 and 2, for instance, and pass them down to segment '4'... I feel like this might be achievable using the extract method, and then manually writing in those pieces of data before my next segment executes - but again I'm shooting a little in the dark on that.
If anyone has a better approach, or can give me some brief code on just extracting these 'segments' out of a file, I would be ecstatic.
cheers
ETA: It dawns on me that I can probably pose this question a little more simply: If there isn't a 'simple' way to do the above, is there a way to handle a String in the exact same way that 'require()' and 'include()' handle a File?
<?php
$str = file_get_contents('filename.php');
// get values from starting characters
$php_full = array_filter(explode('<?php', $str));
$php = array_filter(explode('<?', $str));
$html = array_filter(explode('?>', $str));
// remove values after last expected characters
foreach ($php_full as $key => $value) {
$php_full_result[] = substr($value, 0, strpos($value, '?>'));
}
foreach ($php as $key => $value) {
if( strpos($value,'php') !== 0 )
{
$php_result[] = substr($value, 0, strpos($value, '?>'));
}
}
$html_result[] = substr($str, 0, strpos($str, '<?'));
foreach ($html as $key => $value) {
$html_result[] = substr($value, 0, strpos($value, '<?'));
}
$html_result = array_filter($html_result);
echo '<pre>';
print_r($php_full_result);
echo '</pre>';
echo '<pre>';
print_r($php_result);
echo '</pre>';
echo '<pre>';
var_dump($html_result);
echo '</pre>';
?>
This will give you 3 arrays of file segments you want, not the exact format you wanted but you can easily modify this arrays to your needs.
For "I'd like to break all of my '$GLOBALS' variables out into their 'simple' names" part you can use extract like
extract($GLOBALS);
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.
Hi I have a javascript pre-load script that incorporates a php command to pre-load uploaded images for a future onClick action. Anyway I am having trouble removing the LAST comma from the last image the pre-load script pulls from.
Here is the script:
<div style="display:hidden">
<script type="text/javascript">
<!--//--><![CDATA[//><!--
var images = new Array()
function preload() {
for (i = 0; i < preload.arguments.length; i++) {
images[i] = new Image()
images[i].src = preload.arguments[i]
}
}
preload(
<?php
for ($i = 0; $i < 6; $i++) {
if (!empty($imgs[$i])) {
echo "'http://www.samplegallery.com/upload/image.php?img_source_url=" . $imgs[$i] . "&img_resize_to=500',";
}
}
?>
)
//--><!]]>
</script>
</div>
Anyway, I need to find out how to remove the last comma from the last image that is uploaded. Not sure how to do this. Please help me! BTW... the images don't have an extension since they are linking to a php image script that resizes them and places them into a watermark. Hope you guys can help me figure!
Think the other way! the comma could be at the start and then you remove it from the first one :)
if (!empty($imgs[$i])) {
$comma = $i == 0? '' : ',';
echo $comma."'http://www.samplegallery.com/upload/image.php?img_source_url=" . $imgs[$i] . "&img_resize_to=500'";
}
This way doesn't matter if $i is equal to 5, 8 or 139871!
The easiest way to do it is using the php build in implode() function
<?php echo implode(',', $imgs); ?>
And if you want just the first 6 images, you can make an array like so
$imgs = array_slice($imgs, 0, 6);
So the whole thing must look like that:
preload(
<?php
$imgs = array_slice($imgs, 0, 6);
echo implode(',', $imgs);
?>
)
to remove the last comma from a string, just use this:
$string = rtrim($string, ",");
May I suggest a slightly different approach to your problem? Whenever you want to pass data to JavaScript, JSON is probably the thing you want to generate. json_encode() helps you with that. Your script could look like:
var urls = <?php echo json_encode(array_values($imgs)); ?>;
var images = [];
function preload(urls) {
for (var i = 0; i < urls.length; i++) {
var url = 'http://www.samplegallery.com/upload/image.php?img_source_url='
+ encodeURIComponent(urls[i])
+ '&img_resize_to=500';
images[i] = new Image();
images[i].src = url;
}
}
preload(urls);
please note that I've taken the liberty of adding missing semicolons and var declarations to keep your variables local. I have added the array_values() call to make sure you're passing a numerically indexed array, rather than an associative array that would have resulted in an object literal { ... } rather than an array literl [ ... ].
I have also moved the URL building to JavaScript, as I didn't see a reason to keep it in PHP. If you need this to be in PHP and want to avoid the "manual" loop, look into array_map().
Please also note that I'm running your URL fragment through encodeURIComponent() to properly escape whatever it is you're passing in.
A note on security: should your script at /upload/image.php accept arbitrary URLs (and or "local file resources"), consider white-listing the allowed domains and paths.
You could use a foreach...
<?PHP
if ($count = count($imgs)) {
foreach ($imgs as $key=>$img) {
echo "http://www.samplegallery.com/upload/image.php?img_source_url="
. $img . "&img_resize_to=500";
if ($key < $count-1) echo ",";
}
}
?>