I've made some progress with my regex that I'm using to extract attributes from pseudo-xml-tags, but then I got ambitous and wanted to correctly handle quoted attributes (with quotes being optional):
regex
~\{language\s*=\s*(P?<quote>[\"\']*)(?P<att>.*?)(?P=quote)\s*/\}~
(this is the output of the var that is used as arg in preg_match, so 'sensible things' such as \" were created with chr(92) . chr(34) beforehand...)
input
kjkjkjkjkjkj{language= 'DE' /}xxxxlxlxlxlllllk
extracts 'DE' when testing with RegexBuddy. But PHPs preg_match issues a warning: Warning: preg_match(): Compilation failed: reference to non-existent subpattern at offset 56.
What's the problem? I thought "quote" was assigned before...
Here's the complete program, just in case I have a PHP-error somewhere:
<?php
$QQ=chr(92) . chr(34);
$delimeters = "{}";
$del0 = preg_quote($delimeters{0});
$del1 = preg_quote($delimeters{1});
$tag="language";
$string="fdfdfdfdf{language=1}testhgg";
$preg1 = "|" . $del0 . $tag . "[^" . $del1 . "]*" . $del1 . "(.*?)" . $del0 . "/" . $tag . $del1 . "|";
$preg2 = "~" . $del0 . $tag . "\s*=\s*(?P<" . "quote>[" . $QQ . "\']*)(?P<att>.*?)(?P=quote)\s*/" . $del1 . "~";
$match=array();
preg_match($preg1,$string,$match);
echo "<br>match1:<pre>";var_dump($match);echo"</pre>";
$match=array();
preg_match($preg2,$string,$match);
echo "<br>match2:<pre>";var_dump($match);echo"</pre>";
?>
Your named subpattern is formatted incorrectly.
(P?<quote>[\"\']*)
should be
(?P<quote>[\"\']*)
See http://php.net/manual/en/regexp.reference.subpatterns.php
Related
while trying to generate dynamic sitemaps, I tried adding two variables in url path, and the line is giving me error
this is my sample line:
echo "<loc>" . $base_url . "category.php?category=" . $subFeaturedPostCatSlug . "&job=" . "$subFeaturedPostSlug" . "</loc>" . PHP_EOL;PHP_EOL;
I tried doing it like this also:
echo "<loc>{$base_url}category.php?category={$subFeaturedPostCatSlug}&job={$subFeaturedPostSlug}</loc>" . PHP_EOL;
error screenshot attached;
Any help will be appreciated, thanks in advance
Try this -
$str = $base_url . "category.php?category=" . $subFeaturedPostCatSlug . "&job=" . $subFeaturedPostSlug . "" . PHP_EOL;
echo htmlspecialchars_decode($str);
You should be able to fix this using the urlencode() function as mentioned in your comments.
So,
echo "<loc>" . $base_url . "category.php?category=" . $subFeaturedPostCatSlug . "&job=" . "$subFeaturedPostSlug" . "</loc>" . PHP_EOL;PHP_EOL;
becomes
echo "<loc>" . urlencode($base_url) . "category.php?category=" . urlencode($subFeaturedPostCatSlug) . "&job=" . urlencode($subFeaturedPostSlug) . "</loc>" . PHP_EOL.PHP_EOL;
More details at PHP Documentation for urlencode()
Also, I found out that there is error in your code:
echo "<loc>" . $base_url . "category.php?category=" . $subFeaturedPostCatSlug . "&job=" . "$subFeaturedPostSlug" . "</loc>" . PHP_EOL;PHP_EOL;
Towards the end of the echo, you have written:
...PHP_EOL;PHP_EOL;
which should ideally have been
...PHP_EOL.PHP_EOL;
I need search and highlight the word.
My sentence is
Please see our Author Guide for more information: http://digital-library.theiet.org/journals/author-guide.
you will be contacted shortly asking you to take a decision and sign either a copyright or Open Access licence form.
My code
function find_highlight_word($word) {
$text = preg_replace_callback($word, function($matches) use (&$counter) {
$counter++;
return '<b class="search_mark highlighted" id="matched_' . $counter . '">'
. substr($matches[0], 0, strlen($matches[0]))
. '</b>';
}, $text);
return $text;
}
$word = '//';
$word = '/' . preg_quote($word) . '/i';
$this->find_highlight_word($word);
When I'm searching with '//' that time showing php error.
You're correctly attempting to preg-quote your string, but you're not telling it what your delimiter is, so the // inside the string is causing issues. Pass the used delimiter as the second argument, so it can be escaped as well:
$word = '/' . preg_quote($string, '/') . '/i';
I have following string (using $_POST), how to remove all the new line, spaces and make it as a absolute single line?
Physical Address. . . . . . . . . : E8-6A-64-DE-48-60
Physical Address. . . . . . . . . : 04-EA-56-08-E6-8F
Physical Address. . . . . . . . . : 06-EA-56-08-E6-8E
Physical Address. . . . . . . . . : 04-EA-56-08-E6-8E
Physical Address. . . . . . . . . : 04-EA-56-08-E6-92
Not always stable?
$request= mysql_real_escape_string(trim($_POST['request']));
$request_sql =str_replace("\r\n",'', $request);
$request_sql = str_replace("\\r\\n",'', $request_sql);
echo $request_sql;
trim only strips spaces at end and start of the string and you should strip \r and \n individually.
try this:
$request= mysql_real_escape_string($_POST['request']);
$request_sql =str_replace("\n",'', $request);
$request_sql = str_replace("\r",'', $request_sql);
$request_sql = str_replace(" ",'', $request_sql);
echo $request_sql;
Try using regex.
$request = $_POST['request'];
//Remove all characters that are not A-Z, a-z, 0-9 or '.', ':' or '-'
$request_sql = preg_replace("/[^A-Za-z0-9.:-]/", '', $request );
try:
$str = 'Physical Address. . . . . . . . . : E8-6A-64-DE-48-60
Physical Address. . . . . . . . . : 04-EA-56-08-E6-8F
Physical Address. . . . . . . . . : 06-EA-56-08-E6-8E
Physical Address. . . . . . . . . : 04-EA-56-08-E6-8E
Physical Address. . . . . . . . . : 04-EA-56-08-E6-92';
echo str_replace(" \n", '', $str);
Output:
Physical Address. . . . . . . . . : E8-6A-64-DE-48-60Physical Address. . . . . . . . . : 04-EA-56-08-E6-8FPhysical Address. . . . . . . . . : 06-EA-56-08-E6-8EPhysical Address. . . . . . . . . : 04-EA-56-08-E6-8EPhysical Address. . . . . . . . . : 04-EA-56-08-E6-92
After replacing the \n you can use mysql escape string to avoid sql injection.
The problem with your code is that mysql_real_escape_string will not only escape ' and " but it will escape other characters like \n and \r which you want to remove.
It will replace new line characters with a backslash character followed by l characters
so removing newlines, carriage return after they have been escaped will result in a string with extra backslashes \ and n and r characters.
Check out this
<?php
$originalString =
"Line1
Line2
";
// CASE 1 WRONG RESULT
$string1 = mysqli_real_escape_string($con, $originalString);
$string1 = str_replace("\n", '', $string1);
echo "escape then replace result \n";
echo $string1 . "\n";
//CASE 2 EXPECTED RESULT
$string2 = str_replace("\n", '', $originalString);
$string2 = mysqli_real_escape_string($con, $string2);
echo "replace then escape result \n";
echo $string2 . "\n";
this will output
escape then replace result
Line1\nLine2\n
replace then escape result
Line1Line2
So to correct your code
$request_sql =str_replace(["\n", "\r", " "],'', $_POST['request']);
$request= mysql_real_escape_string($request_sql);
echo $request_sql;
Please don't use mysql_real_escape_string , instead use prepared statements, here an answer for how to switch to them, they will make your life much more easier and safer.
I am trying to write file but just need to work out best way to make some gaps/spaces between some code “[‘default’][‘hostname’]” space . ‘=’ . space “‘localhost’”can not work it out.
At the moment when reload page it produces $db['default']['hostname']='localhost'; but need gap/space $db['default']['hostname'] = 'localhost';
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index(){
$output = '<?php' . "\n";
$output .= "\n";
$output .= '// DB' . "\n";
$output .= '$db' . "['default']['hostname']" space . '=' . space "'localhost'". ";" . "\n";
$file = fopen(APPPATH . 'config/database-test.php', 'w');
fwrite($file, $output);
fclose($file);
$this->load->view('welcome_message');
}
}
$output .= '$db' . "['default']['hostname']" space . '=' . space "'localhost'". ";" . "\n";
This line has syntax errors because you're missing the concat operator before the first space and after the second one (whatever the space is meant to be).
Instead of complicating things, why don't you just write this and avoid the concat hell:
$output .= '$db' . "['default']['hostname'] = 'localhost';\n";
I think I have found answer to make gap . " " . seems to do the trick.
$output .= '$db' . "['default']['hostname']" . " " . '=' . " " . "'localhost'". ";" . "\n";
As an additional note, I see you are writing a config file to be processed later.
I have found output buffering and var_export to do this job exceptionally well.
ob_start();
var_export($config);
$out = ob_get_clean();
then
fwrite($f, '$config = '.$out.';'); etc...
http://us3.php.net/manual/en/function.ob-get-clean.php
http://us3.php.net/manual/en/function.var-export.php
basically this will turn an array into a parse-able string such as
array(
'default'=>array(
'hostname'=>'localhost',
'user' => 'user', ///etc
)
)
then just add the variable part and the ending semi-colon
if you plan to do multiple values this would be a much cleaner approach
EDIT: Fixed, I had to remove all spaces I had (before the commas in the code below) and use trim
I'm trying to generate a CSV file using PHP. However the file splits into lines on its own. It looks fine in View Source, but excel/notepad show lines randomly broken up.
Here is my code :
// Echo Code Here
$string = '"REF1" , "FIRSTCLASS" ,"P" ,"1" ,"' . $orderref . '" ,"' . $fullname . '" ,"' . $add1 . '" ,"' . $add2 . '" ,"" ,"' . $postcode . '" ,"' . $city . '" ,"' . $country . '" ,"' . $fullname . '" ,"' . $telephone . '" ,"' . $email . '" ,"1" ,"1.0 kg"' . "\n";
echo $string;
Any help would be great, this is my first time working with CSV in PHP.
Since you're using Windows, you should use Windows-style line endings: \r\n instead of just \n.
There is PHP function forwriting CSV into file:
PHP "fputcsv" function
http://us2.php.net/manual/en/function.fputcsv.php