I have a textbox that i use nl2br to insert into db. The problem is that is adding too much space. Is there a way to assign this an id while being processed or some other way to reduce the space between sentences?
Looks like what you want is some cleanups of those lines.
This is what I use on my php apps.
<?php
function trimlines($html){
$ret = str_replace("\r\n","\n",$html);
$ret = explode("\n",$ret);
$ret = array_map('trim',$ret);
$ret = join("\r\n",$ret);
return $ret;
}
?>
Regular expressions can also be used, but for quick cleanups, this works well for me.
Hope it helps :)
I would suggest wrapping the variable in sql with TRIM('sentences here')
Related
Coming from a field of a database I get a result that looks like this
I require that this result is interpreted as html in a view, for this I use the function html_entity_decode($value, ENT_QUOTES, 'UTF-8') that achieves the next output.
At this point I hope that the content is displayed as HTML but it does not happen that way, could you please guide me what is the correct way to achieve this.
I'm dealing with this, which at first seemed like a simple matter. and probably it is. I think it's probably that I'm missing something. In any case, I welcome any comments
Thanks in advance
I got the solution to this problem thanks to a series of filters, I describe the solution below, I hope it will be useful to someone
$html = html_entity_decode($value, ENT_COMPAT, 'UTF-8');
$html = stripslashes($html);
$html = str_replace('rn', '<br>', $html);
$html = html_entity_decode($html);
echo $html;
This is the final output
Your data most likely is JSON, you can see it because the normal slash / is escaped \/, which is required in JSON notation.
So it is much better to simply use json_decode() instead of parsing the data yourself.
Try this instead:
$html = json_decode($value);
I am making a website, and I would like to make a custom Markup type language in PHP. I want the tags to be surrounded with [ and ]. Now, I was thinking about this, like anyone would, and I could do something like this:
function formatMarkup($markup = ''){
$markup = str_replace('[color=blue]', '<span style="color: blue">', $markup);
return $markup
}
Even though that might work, it would be more progrematically correct if it would do something like explode(), but starting at every [ and ending at every ]. This would be great if I found out. Thank you for your time and effort.
EDIT:
I have decided to use preg_split(). It seems nice, and all, but I cannot get the regex. Here is my code.
EDIT #2:
I have got most of the regex done, but there are uneeded extra keys in the array. How would I fix them? Here is my new code.
I have made my Markup language. I used
$split = preg_split("/(\[|\])/", $markup);
to get the individual "tags" and used
foreach($split as $k => $v){
if(strlen($v) < 1){
continue;
}
to illiterate through each of them, and check if the value is empty. Then, after that, I would do all of my checks, and parse the code blocks together, and make line, after line, the re-constructed text.
I am trying to append a URL if necessary, and skip over it when not necessary. The think is, I'm learning php right now and I would like to use regular expressions as much as possible. would it be possible to make this code more concise using preg_match? Example:
<?php
$facebook_url = str_replace("facebook.org","facebook.com", trim($_REQUEST['facebook_url']));
$position = strpos($facebook_url, "facebook.com");
if ($position === false) {
$facebook_url = "http://www.facebook.com/" . $facebook_url;
}
?>
But using:
if (!preg_match("/^(http:///www.facebook.com | facebook.com)/i"), $facebook_url)) {
$facebook_url = "http://www.facebook.com/" . $facebook_url;
}
I feel like that should work the way I understand php syntax, but something isn't working right. Thank you in advance.
I don't know why you would want to use regex "as much as possible" as opposed to as much as is needed, which should be very little. In your case the original code is much faster, and you can still do it with less code:
if (stripos($facebook_url, "facebook.com") === false) {
Your regex would require a space after the .com or before the facebook in the alternation. Space matters in regex.
I was creating a Syntax Highlighter in PHP but I was failed! You see when I was creating script comments (//) Syntax Highlighting (gray) , I was facing some problems. So I just created a shortened version of my Syntax Highlighting Function to show you all my problem. See whenever a PHP variable ,i.e., $example, is inserted in between the comment it doesn't get grayed as it should be according to my Syntax Highlighter. You see I'm using preg_replace() to achieve this. But the regex of it which I'm using currently doesn't seem to be right. I tried out almost everything that I know about it, but it doesn't work. See the demo code below.
Problem Demo Code
<?php
$str = '
<?php
//This is a php comment $test and resulted bad!
$text_cool++;
?>
';
$result = str_replace(array('<','>','/'),array('[',']','%%'),$str);
$result = preg_replace("/%%%%(.*?)(?=(\n))/","<span style=\"color:gray;\">$0</span>",$result);
$result = preg_replace("/(?<!\"|'|%%%%\w\s\t)[\$](?!\()(.*?)(?=(\W))/","<span style=\"color:green;\">$0</span>",$result);
$result = str_replace(array('[',']','%%'),array('<','>','/'),$result);
$resultArray = explode("\n",$result);
foreach ($resultArray as $i) {
echo $i.'</br>';
}
?>
Problem Demo Screen
So you see the result I want is that $test in the comment string of the 'Demo Screen' above should also be colored as gray!(See below.)
Can anyone help me solve this problem?
I'm Aware of highlight_string() function!
THANKS IN ADVANCE!
Reinventing the wheel?
highlight_string()
Also, this is why they have parsers, and regex (despite popular demand) should not be used as a parser.
I agree, that you should use existing, parsers. Every ide has a php parser, and many people have written more of them.
That said, I do think it is worth the mental exercise. So, you can replace:
$result = preg_replace("/(?<!\"|')[\$](?!\()(.*?)(?=(\W))/","<span style=\"color:green;\">$0</span>",$result);
with
//regular expression.:
//#([^(%%%%|\"|')]*)([\$](?!\()(.*?)(?=(\W)))#
//replacement text:
//$1<span style=\"color:green;\">$2</span>
$result = preg_replace("#([^(%%%%|\"|')]*)([\$](?!\()(.*?)(?=(\W)))#","$1<span style=\"color:green;\">$2</span>",$result);
Personally, I think your best bet is to use CSS selectors. Replace style=\"color:gray;\" with class="comment-text" and style=\"color:green;\" with class="variable-text" and this CSS should work for you:
.variable-text {
color: #00E;
}
.comment-text .comment-text.variable-text {
color: #DDD;
}
Insert don't use regex to parse irregular languages here
anyway, it looks like you've run into a prime example of why regular expressions are not suited for this kind of problem. You'd be better off looking into PHP's highlight_string functionality
Well, you don't seem to care that php already has a function like this.
But because of the structure of php code one cannot simply use a regex for this or walk into mordor (the latter being the easier).
You have to use a parser or you will fly over the cuckoo's nest soon.
I would like to strip everything in a string before a - and leave what's after that. For example:
15.11-101
I want to strip 15.11 and leave 101. I've tried a few different things, but can't seem to get it working, if anyone could help me out, that'd be fantastic :)
Cheers
Leanne
You could use something like this:
$result = substr($original, strpos($original, '-') + 1);
assuming it appears only once, you can use the explode function.
$s='15.11-101';
$a=explode('-',$s,1);
echo $a[1];
This should be the fastest way.