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/>"; }
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'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
Guys i just wanna ask if there's a possibility: for example $currenttext = "someexamplepls";
if you would like to show or print it just use print "$currenttext"; then the prepared output look like this someexamplepls. My question is if i would like to print it like this:
somee
xampl
epls
in every 5 character there is automatic newline what should I do?
Use wordwrap():
echo wordwrap($str, 5, "\n", true);
In case you want HTML linebreaks use <br> instead of \n.
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.
I want to change
<lang class='brush:xhtml'>test</lang>
to
<pre class='brush:xhtml'>test</pre>
my code like that.
<?php
$content="<lang class='brush:xhtml'>test</lang>";
$pattern=array();
$replace=array();
$pattern[0]="/<lang class=([A-Za-z='\":])* </";
$replace[0]="<pre $1>";
$pattern[1]="/<lang>/";
$replace[1]="</pre>";
echo preg_replace($pattern, $replace,$content);
?>
but it's not working. How to change my code or something wrong in my code ?
There's quite a few problems:
Pattern 0 has the * outside the group, so the group only matches one character
Pattern 0 doesn't include the class= in the group, and the replacement doesn't have it either, so there won't be a class= in the replaced string
Pattern 0 has a space after the class, but there isn't one in the content string
Pattern 1 looks for lang instead of /lang
This will work:
$pattern[0]="/<lang (class=[A-Za-z='\":]*) ?>/";
$replace[0]="<pre $1>";
$pattern[1]="/<\/lang>/";
$replace[1]="</pre>";
How bout without regex? :)
<?php
$content="<lang class='brush:xhtml'>test</lang>";
$content = html_entity_decode($content);
$content = str_replace('lang','pre',$content);
echo $content;
?>
Using preg_replace is a lot faster than str_replace.
$str = preg_replace("/<lang class=([A-Za-z'\":]+)>(.*?)<\/lang>/", "<pre class=$1>$2</pre>", $str);
Execution time: 0.039815s
[preg_replace]
Time: 0.009518s (23.9%)
[str_replace]
Time: 0.030297s (76.1%)
Test Comparison:
[preg_replace]
compared with.........str_replace 218.31% faster
So preg_replace is 218.31% faster than the str_replace method mentioned above. Each tested 1000 times.