How to replace plus sign (+) and still not replace %2B using php? - php

This is my code for test.php
<?PHP
include("connect.php");
$search_value = mysqli_real_escape_string($db_mysqli,$_GET['search_value']);
$search_value_show_text = preg_replace('/\+/', ' ', $search_value);
echo $search_value_show_text;
?>
When i test www.example.com/test?search_value=%2B+%2B
It's not echo anything (i mean it's show 3 spaces).
I want to show + +
How can i do ?

You seem not understand what you are doing. Plus sign in url means space, so that's one. Then you replace two other pluses (which are automatically decoded from %2B form) with spaces and you end up with three spaces total. Simply do nothing (or use urldecode() if needed) with your inpit data and you should be good.
Also you use mysql related ed calls w/o using mysql - thats wrong

$search_value = 'www.example.com/test?search_value=%2B+%2B';
$search_value_show_text = preg_replace('/\+/', ' ', $search_value);
echo $search_value_show_text ."<br/>";
$search_value_show_text = preg_replace('/\%2B/', '+', $search_value_show_text);
echo $search_value_show_text;
output:
www.example.com/test?search_value=%2B %2B
www.example.com/test?search_value=+ +

Related

How to use preg_replace with url encoded $_GET?

I have a an url which looks like this https://URL.DOMAIN/blog.php?id=43&q=echo%20%27test%27.
When I use <?php echo $_GET['q'] ?> it displays echo 'test' which is what I want.
I am using this variable inside a preg_replace function which is basically made to apply a yellow background under matched strings:
preg_replace('/\b('.$_GET['q'].')\b/iu', '<span class="research-news-found">$1</span>', $news_content);
It works perfectly for "normal" strings like "apple" or whatever, but when there is a ' inside the search query it doesn't match anything.
Code example
$news_content = $news_display['news_description'];
if(isset($_GET['q'])){
$news_content = preg_replace('/\b('.$_GET['q'].')\b/iu', '<span class="research-news-found">$1</span>', $news_content);
}
$news_display['news_description'] contains the text output from DB.
Just make the pattern greedy ? and remove the trailing word boundary \b since ' is not a word character and is a word boundary:
$news_content = preg_replace('/\b('.$_GET['q'].'?)/iu',
'<span class="research-news-found">$1</span>',
$news_content);
Demo
But if you are hoping that it will actually echo test, then no. You would need to restructure your question to state what you want to achieve, not how to get this replacement to work.

(PHP/Wordpress) Remove characters easily, but not '$' when string is output of shortcode

NOTE: I've answered my own question below, but posting this anyway for someone else in my situation to learn...
This is driving me insane.
Here's the scenario. There are a million questions here about removing characters like '$' from simple strings (eg. $string = '$10.00') and I can do that just fine.
However... when the string originates from shortcode output (eg. $string = do_shortcode($mycode), I just can't do it. I can remove the 1's and 0's and .'s but the $ won't budge.
Note: The shortcode I'm using is 'Wordpress Currency Switcher' (wpcs_price). My newbie mind tells me that the problem is my string isn't a string at all, but live code or some weird type of array I don't understand.
Example.
function justwork() {
$rawprice = 10.00;
$rawpriceSC = '[wpcs_price value=\''.$rawprice.'\']';
$rawpriceOUT = do_shortcode($rawpriceSC); /* This will output $10.00 */
$finalprice = str_replace('$','',$rawpriceOUT);
echo $finalprice;
}
add_shortcode('justwork', 'justwork');
This will result in:
'$10.00' ($ not removed)
Using the same code on a different character (Eg. the '.') works just fine.
Eg.
$finalprice = str_replace('.','',$rawpriceOUT);
And the output will be:
'$1000'
The $ just won't budge. I've tried substr,trim,preg, lots of other stuff, and I soon realised all is not what it seems, it's carrying some baggage. So I tried capturing the output buffer, and still the resultant string behaved odd.
Any help... oh gosh please, going insane on this one.
ANSWER
While reviewing all the methods I'd looked at, one came up I hadn't.
echo htmlentities($finalprice);
And guess what this output?
<span class="wpcs_price" id="wpcs_58c296db36aa3" data-amount=10 ><span class="wpcs_price_symbol">$</span>10.00</span>
OK. So either I need to use & # 3 6 ; instead of $, or I can operate on that whole mess to get my 10.00. Geeze.
Confirmed that simply replacing the '$' with '& # 3 6 ;' (remove spaces) works fine.
function justwork() {
$rawprice = 10.00;
$rawpriceSC = '[wpcs_price value=\''.$rawprice.'\']';
$rawpriceOUT = do_shortcode($rawpriceSC);
$price = str_replace('$', '', $rawpriceOUT) ;
echo $price;
}
add_shortcode('justwork', 'justwork');

extract info from another web page

I have this test.php where i have this info :
callername1 : 'Fernando Verdasco1'
callername2 : 'Fernando Verdasco2'
callername3 : 'Fernando Verdasco3'
callername4 : 'Fernando Verdasco4'
callername5 : 'Fernando Verdasco5'
this page automatically changes that name every 10 min
In this another page test1.php
I need a php code that takes only the name of the callername3 and echo'it
Fernando Verdasco3
I've tried this like so test1.php?id=callername3
<?php
$Text=file_get_contents("test.php");
if(isset($_GET["id"])){
$id = $_GET["id"];
parse_str($Text,$data);
echo $data[$id];
} else {
echo "";
}
?>
but no result.
Is there any other option?
If i have "=" instade of ":"
callername1 = 'Fernando Verdasco1'
callername2 = 'Fernando Verdasco2'
callername3 = 'Fernando Verdasco3'
callername4 = 'Fernando Verdasco4'
callername5 = 'Fernando Verdasco5'
And i use This php Code it works
<?php
$Text=file_get_contents("test.php")
;preg_match_all('/callername3=\'([^\']+)\'/',$Text,$Match);
$fid=$Match[1][0];
echo $fid;
?>
i need this to work with ":"
Help?
You should store data in a file with the .php extension, since it's not executable PHP. I looks like you're going for the JSON syntax.
Since you need it to work with ':' I assume, for whatever reason, you can't change the format. Your example with '=' works because of the regexp:
preg_match_all('/callername3=\'([^\']+)\'/',$Text,$Match);
This says, match text like callername3= followed by a ' followed by one or more chars that are not a ' followed by a final '. Everything between the 's is stored in $Match[1][0] (if there were more parts in brackets they be stored in $Match[2][0], etc).
Your example doesn't work since it doesn't account for the spaces before and after the = sign. But we can fix that up and change it to work for : like this:
preg_match('/callername3\s*:\s*\'([^\']+)\'/',$Text,$Match);
echo $Match[1] ."\n";
This displays:
Fernando Verdasco3
And what that regular expression is match text that start callername3 followed by any amount of whitespace (that's the \s*) followed by a :, followed by any amount of whitespace, followed by a name in quotes (that is stored in $Match[1], this is the area of the regular expression enclosed in parenthesis).
I've also used just preg_match because it looks like you only need to match one example.
There is a rather simple approach to tihs:
$fData = file_get_contents("test.php");
$lines = explode("\n", $fData);
foreach($lines as $line) {
$t = explode(":", $line);
echo trim($t[1]); // This will give you the name
}

Why is this not making a plus sign?

I have:
$str = 'test%2B';
echo html_entity_decode($str);
I want it to return test +
What am I doing wrong?
NOTE: Sorry, the string cannot be modified. It's from an external source, I just need to make it replace the %2B with + signs somehow with PHP.
You didn't escape the space, and you should be using urldecode instead of html_entity_decode.
Try
$str = 'test%20%2B';
echo urldecode($str); // test +
If you wish to use html_entity_decode, use +:
$str = 'test +';
echo html_entity_decode($str); // test +
EDIT: If you need to decode a url that you cannot change yourself, urldecode should still work fine.
That string is encoded for a URL, not with HTML entities.
You need urldecode.
echo urldecode($str); // "test +"
An HTML-encoded string would look like this: test +, because none of those characters need HTML-encoding.
Try + instead. In your example, you are using URL encoding syntax and not HTML entity syntax.
In html a + is +. Try
$str = 'test +';
$str = "test %2B";
echo urldecode($str);

preg_replace between > and <

I have this:
<div> 16</div>
and I want this:
<div><span>16</span></div>
Currently, this is the only way I can make it work:
preg_replace("/(\D)(16)(\D)/", "$1<span>$2</span>$3", "<div> 16</div>")
If I leave off the $3, I get:
<div><span>16</span>/div>
Not quite sure what your after, but the following is more generic:
$value = "<div> 16 </div>";
echo(preg_replace('%(<(\D+)[^>]*>)\s*([^\s]*)\s*(</\2>)%', '\1<span>\3</span>\4', $value));
Which would result in:
<div><span>16</span></div>
Even if the value were:
<p> 16 </div>
It would result in:
<p><span>16</span></p>
I think you meant to say you're using the following:
print preg_replace("/(\\D+)(16)(\\D+)/", "$1<span>$2</span>$3", "<div>16</div>");
There's nothing wrong with that. $3 is going to contain everything matched in the second (\D+) group. If you leave it off, obviously it's not going to magically appear.
Note that your code in the question had some errors:
You need to escape your \'s in a string.
You need to use \D+ to match multiple characters.
You have a space before 16 in your string, but you're not taking this into account in your regex. I removed the space, but if you want to allow for it you should use \s* to match any number of whitespace characters.
The order of your parameters was incorrect.
Try following -
$str = "<div class=\"number\"> 16</div>";
$formatted_str = preg_replace("/(<div\b[^>]*>)(.*?)<\/div>/i", "$1<span>$2</span></div>", $s);
echo $formatted_str;
This is what ended up working the best:
preg_replace('/(<.*>)\s*('. $page . ')\s*(<.*>)/i', "$1" . '<span class="curPage">' . "$2" . '</span>' . "$3", $pagination)
What I found was that I didn't know for sure what tags would precede or follow the page number.

Categories