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
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";
}
i use (str_replace) function to replace ##ID## in youtube url with this regular expression : (?P<id>[a-z-A-Z_0-9]+)
so i use this code to do this :
<?php
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo $lastchange;
?>
i get the output in the browser like this : https://www.youtube.com/watch?v=(?P[a-z-A-Z_0-9]+), its looks like <id> not show up !
i try this simple code :
<?php
echo "This is my <id>";
?>
but i just get this is my in the browser !
What's the probleme ? and how i can fix it , thanks
is being interpreted as HTML so your browser is parsing it and since it is not a renderable element, it shows nothing. Try:
<?php
echo "This is my <id>
?>
As for the str_replace, it's doing exactly what the function is supposed to be doing. If you're looking to use regular expressions in string replacements, use preg_replace
The tag <id> is being removed by your browser. It is really there if you watch the source code. Maybe you should try:
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo urlencode( $lastchange );
Problem is with the line:
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
str_replace does not use regex.
You will need preg_replace
$pattern = '(<id>[a-z-A-Z_0-9]+)'
$replacement = '##ID##'
$string = $urlbase
$lastchange = preg_replace($pattern, $replacement, $string);
Also < and > are html entities which means they are reserved chars for HTML they have some special meanings if you want to show them then you must use there entity name eg < and > in your case respectively.
<?php
echo " echo "This is my <id>";
?>
How can I find and replace the same characters in a string with two different characters? I.E. The first occurrence with one character, and the second one with another character, for the entire string in one go?
This is what I'm trying to do (so users need not type html in the body): I've used preg_replace here, but I'll willing to use anything else.
$str = $str = '>>Hello, this is code>> Here is some text >>This is more code>>';
$str = preg_replace('#[>>]+#','[code]',$str);
echo $str;
//output from the above
//[code]Hello, this is code[code] Here is some text [code]This is more code[code]
//expected output
//[code]Hello, this is code[/code] Here is some text [code]This is more code[/code]
But problem here is, both >> get replaced with [code]. Is it possible to somehow replace the first >> with [code] and the second >> with a [/code] for the entire output?
Does php have something to do this in one go? How can this be done?
$str = '>>Hello, this is code>> Here is some text >>This is more code>>';
echo preg_replace( "#>>([^>]+)>>#", "[code]$1[/code]", $str );
The above will fail if something like the following is your input:
>>Here is code >to break >stuff>>
To deal with this, use negative lookahead:
#>>((?!>[^>]).+?)>>#
will be your pattern.
echo preg_replace( "#>>((?!>[^>]).+?)>>#", "[code]$1[/code]", $str );
I am currently trying to echo a text value from a variable which contains html-style tags. <...>
$string = "variable_name";
$tag_str = "<".$string.">";
echo $tag_str;
currently this echo's as nothing as it believes it is html code. How would I go about echoing <variable_name> to the page so it is viewable and not interpreted as code by the browser?
You'll have to html encode your output
$string = "variable_name";
$tag_str = "<".$string.">";
echo htmlspecialchars($tag_str);
The angle brackets (<>) are precisely what tells the browser that it should be treated as HTML code. Instead, output the HTML-encoded versions of those otherwise special characters:
$tag_str = "<".$string.">";
Alternatively, automate this process:
$tag_str = htmlspecialchars("<".$string.">");
Use highlight_string().See below code
$string = "variable_name";
$tag_str = "<".$string.">";
highlight_string($tag_str);
I have the following code:
<p><?php echo $item['desc']; ?></p>
The code pulls the following from the database:
Point 1
Point 2
Point 3
and displays it as: Point 1 Point 2 Point 3,
What do I need to do to get the new lines included, I've tried adding /n or tags into to the DB reference however it is not making any difference.
This should work
<p><?php echo nl2br($item['desc']); ?></p>
Otherwise:-
echo nl2br(str_replace(' ',"\n", $item['desc']));
The newline is included. It's just not displayed in the browser, because the HTML standard says so. If you want it displayed in the browser, change newline to <br> tags, e.g. using nl2br()
Maybe the newlines are being printed, but you need them to be <br> tags, so they will appear as newlines on the webpage? You can use the function nl2br() for that:
<p><?php echo nl2br($item['desc']); ?></p>
Try this
echo str_replace(' ',"\n", $item['desc']);
\n won't work because the string you print has to be processed by the browser in html.
Use <br> tag instead.
Try like
echo str_replace(' ',"<br>", $item['desc']);
I couldn't understand exactly what you want but probably it's something like this:
$string = "Point 1 Point 2 Point 3";
$string = preg_replace("/(point \d+ )/i", "\$1\r\n", $string);
echo $string
Output:
Point 1
Point 2
Point 3
Note: Use only nl2br (see other answers) if the new lines are already in the string but just not displayed.
if your code in one line like TEST TEST TEST you can use preg_replace function :
echo preg_replace("{[ ]+}","<br/>",$items['desc']);
if your string like this TEST 1 TEST 2 ... and your goal is :
TEST 1
TEST 2
... so on..
you can use this pattern:
$t = preg_match_all("{[\w]+[ ]?[0-9]+}",$items['desc'],$m);
foreach ($m[0] as $val) { echo $val."<br/>"; }