I am attempting to make a gallery that calls the image names from a flat file database using the PHP 'fgets' function. There are different sections in the gallery, each with it's own default image, and a small list of images that the users can select from. Everything is working fine, except for one button.
I have one button on the page that is supposed to reset all the galleries to their default images using Javascript OnClick. It works exactly as I want it to, with one small hitch: It copies the line break at the end of the line allong with the characters on the line, breaking the Javascript.
The offending code:
function back(){
document.getElementById('back').className='back';
document.getElementById('one').className='cellcont';
//This should output the proper javascript, but does not
<?php
$a = fopen('c.txt','r');
if (!$a) {echo 'ERROR: Unable to open file.'; exit;}
$b = fgets($a);
echo "document.getElementById('i1').src='$b';";
fclose($a);
?>
}
How it outputs:
function back(){
document.getElementById('back').className='back';
document.getElementById('one').className='cellcont';
document.getElementById('i1').src='00.jpg
';}
As you can see, the ending quotation mark and the semi-colon falls on the next line, and this breaks the button.
With the files I'm using now, I can get around this problem by changing, "fgets($a)" to, "fgets($a, 7)" but I need to have it grab the entire line so that if the client decides to enter a file with a longer name, it does not break the gallery on them.
Use rtrim().
Specifically:
rtrim($var, "\r\n");
(To avoid trimming other characters, pass in just newline.)
Your best bet is to use the php trim() function. See http://php.net/manual/en/function.trim.php
$b = trim(fgets($a));
$b = fgets($a);
$b = preg_replace("/[\n|\r]/",'',$b);
Related
I am trying to hit a URL after generating the data to be filled for the parameters that are passed in URL using Python in back end. So the flow is:
User lands on a page with a form having some drop downs.
Python code in the backend reads the content from a file and returns single output based on some conditions for each of the dropdown.
User hits the submit button with the data.
The data gets generated correctly but when I hit submit button, I get %0D%0A characters at the end of the parameter values in the URL
E.g., sample.php?param1=20%0D%0A¶m2=50%0D%0A
How do I get rid of these values as this is causing trouble with the other code where I am using these values?
I take it you read the data from a file, so probably reading the file causes the line endings to be read as well.
In any case, try using strip() or rstrip() in your Python code to remove all/trailing whitespace before your assemble the target URL.
I understand that it's actually a PHP script that assembles the URL. In that case, use PHP's trim() function on the variables you use to assemble the URL.
For example: Assume that $val1 and $val2 are read from a file or some other place. Then the following line assembles above URL stripping whitespace from $val1 and $val2.
$url = "sample.php?param1=" . trim($val1) . "¶m2=" . trim($val2);
Some browsers do that automatically, you can try decoding it back using urldecode()
http://php.net/manual/en/function.urldecode.php
Try this :
<?Php
$str = "Your Inputed Value or string"
$url = str_replace(" ","-", $str);
?>
Link Menu
I use a form which purpose is to generate a customized .csv file. Among other things, the user can specify the end of line character.
To do so, he simply enters it in a text area. I have a problem with the backslashed characters, and in particular "\n".
If I don't check the string, the "\n" entered by the user isn't evaluated. It is added to the .csv file as a string, and the outputed CSV has only one line with all the values...: value1\nvalue2\n instead of :
value 1
value 2
...
So far, I've found a way to deal with it, making the following test :
if ($var == '\n') {
$var = "\n";
}
Is there a more elegant way to solve this problem ?
I'm writing a script (in PHP) which will go through a PHP file, find all instances of a function, replace the function name with another name, and manipulate the parameters. I'm using get_file_contents() then strpos() to find the positions of the function, but I'm trying to find a good way to extract the parameters once I know the position of the start of the function. Right now I'm just using loop which walks through the next characters in the file string and counts the number of opening and closing parentheses. Once it closes the function parameters it quits and passes back the string of parameters. Unfortunately, runs into trouble with quotes enclosing parentheses (i.e. function_name(')', 3)). I could just count quotes too, but then I have to deal with escaped quotes, different types of quotes, etc.
Is there a good way to, knowing the start of the function, to grab the string of parameters reliably? Thank you much!
EDIT:
In case i didn't read the question carefully, if you want to only get function parameters,you can see these example :
$content_file = 'function func_name($param_1=\'\',$param_2=\'\'){';
preg_match('/function func_name\((.*)\{/',$content_file,$match_case);
print_r($match_case);
but if you want to manipulate the function, read below.
How about these :
read file using file_get_contents();
use preg_match_all(); to get all function inside that file.
please not that i write /*[new_function]*/ inside that file to identify EOF.
I use this to dynamically add/ delete function without have to open that php files.
Practically, it should be like this :
//I use codeigniter read_file(); function to read the file.
//$content_file = read_file('path_to/some_php_file.php');
//i dont know whether these line below will work.
$content_file = file_get_content('path_to/some_php_file.php');
//get all function inside php file.
preg_match_all('/function (.*)\(/',$content_file,$match_case);
//
//function name u want to get
$search_f_name = 'some_function_name';
//
for($i=0;$i<count($match_case[1]);$i++){
if(trim($match_case[1][$i]) == $search_f_name){
break;
}
}
//get end position by using next function start position
if($i!=count($match_case[1])-1){
$next_function= $match_case[1][$i+1];
$get_end_pos = strripos($content_file,'function '.$next_function);
} else {
//Please not that i write /*[new_function]*/ at the end of my php file
//before php closing tag ( ?> ) to identify EOF.
$get_end_pos = strripos($content_file,'/*[new_function]*/');
}
//get start position
$get_pos = strripos($content_file,'function '.$search_f_name);
//get function string
$func_string = substr($content_file,$get_pos,$get_end_pos-$get_pos);
you can do echo $func_string; to know whether these code is running well or not.
Use a real parser, like this one:
https://github.com/nikic/PHP-Parser
Using this library, you can manipulate the source code as a tree of "node" objects, rather than as a string, and write it back out.
I am trying to convert a drupal installation into a front end driven by Code Igniter. This is an experimental project to check the performance boost I can get. But the biggest problem I am facing is that few fields in Drupal store php string as it is. For example
<?php print "A"; ?>
This is normal
Now I am able to see the text "This is normal" which comes from the query that I run in Code Igniter, but I don't see the php function which is saved inside the table. I can see the text when I view the record through phpmyadmin. But somehow not inside the CI query result.
I hope U've got the solution. But just in case if you haven't try this:
I created a table with two columns
//$result Contains the data fetched from the table using a model.
foreach ($result as $key=>$val) {
if($key == 'code') {
$val = str_replace('<?php','',$val); //Remove PHP's opening tag.
$val = str_replace('?>','',$val); //Remove PHP's closing tag.
$val = rtrim($val); //Remove leading and trailing spaces.
echo $key.': ';
eval($val.';'); //Execute the PHP code using eval.
} else {
echo $key.': '.$val.PHP_EOL;
}
}
I tried
echo $result['code']
print_r($result)
var_dump($result)
highlight_string($result['code'])
eval($result['code'])
and finally str_replace followed by eval($result['code']).
Check the screen shot of the result obtained from: here
There you can see that the Result produced by 1,2, and 5 are empty. But when you inspect element against the empty space It'll clearly show that the string that's echoed/print is commented out.
Screen-Shot. This is has nothing to do with codeigniter. Its done by HTML Parser.
So the solution is to remove the opening and closing tags of PHP and then use eval. I hope this helps.
Thanks for the help. Yes, the last resort was the eval function and that is the one which helped me attain which I was wanting it to do.
The data which was inside the database had PHP function and only eval function was able to treat that part as a PHP code and execute it when I was getting the data inside my view.
I'm working on making one of my first wordpress themes, but I seem to be encountering a weird issue. Whenever I call one of my functions with PHP, the return (when viewing the page) has a lot of white space (invisible characters). For some of the things I'm trying to do, it causes problems. Here's an example of one of my functions, the rest are built just like it.
// Get YouTube Username
function soc_youtube() {
global $up_options;
?>
<?php if($up_options->soc_youtube){ ?>
<?php echo $up_options->soc_youtube; ?>
<?php
}
}
That code generated this result:
Update: Fixed
Solution: Use less tags and cut down on breaks in code
Everything outside the php tags is pushed through directly to the output, including all your line breaks. To avoid that, leave the line breaks inside the PHP code:
<?php if($up_options->soc_youtube){
?><?php
echo $up_options->soc_youtube;
?><?php //...
(In your example, I don't see the need to close any of the tags at all, though. You could just have everything inside one set of tags.)
First, there is no need to end a php block and start it right back up again with nothing in between... especially on every single line. Try getting rid of those first and see if that makes a difference:
// Get YouTube Username
function soc_youtube() {
global $up_options;
if($up_options->soc_youtube){
echo $up_options->soc_youtube;
}
}
Next, if that doesn't work, try doing var_dump($up_options->soc_youtube); and see what's there and figure out why.