What I'm doing wrong ?
echo preg_replace('#\d{3}\d{3}\d{3}\d{2}#', '$1.$2.$3-$4', '12345678901');
Output would be this: 123.456.789-01. Isn't formating the string!!
<?php
echo preg_replace('#(\d{3})(\d{3})(\d{3})(\d{2})#', '$1.$2.$3-$4', '12345678901');
is correct, because dollar sign + integer refers to content in () brackets (grouping)
demo
You have not grouped correctly (missing brackets):
echo preg_replace('#(\d{3})(\d{3})(\d{3})(\d{2})#', '$1.$2.$3-$4', '12345678901');
Related
[PHP]I have a variable for storing strings (a BIIGGG page source code as string), I want to echo only interesting strings (that I need to extract to use in a project, dozens of them), and they are inside the quotation marks of the tag
but I just want to capture the values that start with the letter: N (news)
[<a href="/news7044449/exclusive_news_sunday_"]
<a href="/n[ews7044449/exclusive_news_sunday_]"
that is, I think you will have to work with match using: [a href="/n]
how to do that to define that the echo will delete all the texts of the variable, showing only:
note that there are other hrefs tags with values that start with other letters, such as the letter 'P' : href="/profiles... (This does not interest me.)
$string = '</div><span class="news-hd-mark">HD</span></div><p>exclusive_news_sunday_</p><p class="metadata"><span class="bg">Czech AV<span class="mobile-hide"> - 5.4M Views</span>
- <span class="duration">7 min</span></span></p></div><script>xv.thumbs.preparenews(7044449);</script>
<div id="news_31720715" class="thumb-block "><div class="thumb-inside"><div class="thumb"><a href="/news31720715/my_sister_running_every_single_morning"><img src="https://static-hw.xnewss.com/img/lightbox/lightbox-blank.gif"';
I imagine something like this:
$removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n = ('/something regex expresion I think /' or preg_match, substring?);
echo $string = str_replace($removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n,'',$string);
expected output: /news7044449/exclusive_news_sunday_
NOTE: it is not essential to be through a variable, it can be from a .txt file the place where the extracts will be extracted, and not necessarily a variable.
thanks.
I believe this will help her.
<?php
$source = file_get_contents("code.html");
preg_match_all("/<a href=\"(\/n(?:.+?))\"[^>]*>/", $source, $results);
var_export( end($results) );
Step by Step Regex:
Regex Demo
Regex Debugger
To get just the links out of the $results array from Valdeir's answer:
foreach ($results as $r) {
echo $r;
// alt: to display them with an HTML break tag after each one
echo $r."<br>\n";
}
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=+ +
I've got a PHP problem. When I have the following array:
$string = array('<','s');
echo $string[0];
echo $string[1];
Nothing is showing
It prints fine if I put any other special character or integer value in place of the 's'
$string = array('<','1');
echo $string[0];
echo $string[1];
output: <1
OR
$string = array('<','1#');
echo $string[0];
echo $string[1];
output: <#
I assume that your output is not being shown to you as expected because you are looking at it in a web browser. Anything starting with a < character followed by a letter is going to be interpreted as an HTML tag.
If you look at the page source of your output, you will probably see what you are looking for.
I'm sure PHP has a way to output escaped HTML tags and such out to a page, but I'm not familiar with it.
$string = array('<','s');
echo htmlentities($string[0]);
echo htmlentities($string[1]);
when you echo '<', the browser assumes you opened a tag name & expects '>'. as you know, content inside < & > is a tag & doesn't show on output(tags are used for formatting & sometimes styling the page). so, use echo htmlspecialchars($string[0]) instead.
Less than and more than-characters are typically used for defining elements in your html, therefore you must define that these characters should be output like "normal characters" instead:
< = less than (<)
> = more than (>)
In your example:
$string = array(<,'1');
Take a look at http://www.ascii.cl/htmlcodes.htm and look in the "html name column"
You could aslo have a look at http://php.net/htmlentities
I'm trying to build a string of HTML as follows:
$html= "<input name='".GROUP_CONFIG_MAX_CALL_RECORDING_TIME_INPUT."' value='".$MaxCallRecordingTimeSecs."' size='4' ".($bCallRecordingLicensed)?'':'disabled'.">";
But it just gives me a parse error (no specific detail, just that this line is the problem).
I've tried various positioning of quotations and brackets but I'm always getting the parse error. Is this possible the way I'm trying?
$html= "<input name='".GROUP_CONFIG_MAX_CALL_RECORDING_TIME_INPUT."' value='".$MaxCallRecordingTimeSecs."' size='4' ".($bCallRecordingLicensed?'':'disabled').">";
Like codingbiz said, this should work with additional parentheses. I'd go for a more readable version with sprintf though:
$html = sprintf(
'<input name="%s" value="%s" size="4"%s>',
GROUP_CONFIG_MAX_CALL_RECORDING_TIME_INPUT,
$MaxCallRecordingTimeSecs,
( $bCallRecordingLicenced ? '' : ' disabled' )
);
Try
".(($bCallRecordingLicensed)?'':'disabled').">";
additional brackets
Try wrapping the whole ternary in parens, rather than just the variable at the start:
$html= "<input name='".GROUP_CONFIG_MAX_CALL_RECORDING_TIME_INPUT."' value='".$MaxCallRecordingTimeSecs."' size='4' ".($bCallRecordingLicensed?'':'disabled').">";
I think its because you change quote marks:
For example
$test = false;
$strings = "hello ".($test?"you":"")." this is a test";
echo $strings;
works as you expected.
I did find that without the brackets round the test, for example, my test only produced the word "you" .. rather than the whole string - which was odd.
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);