preq_replace automatically adds single quote in Thai language - php

My intention is to breaks groups of 50 chars that do not contain spaces with a \n
My code is like this:
$string= preg_replace('/([^\s<>]{50})(?=[^\s])/u', "$1\n$2", '[ติดต่อนัดหมายชมโครงการหรือสอบถามข้อมูลเพิ่มเติมที่]');
And the result adds \n and additional ' in the new line:
[ติดต่อนัดหมายชมโครงการหรือสอบถามข้อมูลเพิ่มเติมที\n
่]
But with an arbitrary value:
$string= preg_replace('/([^\s<>]{50})(?=[^\s])/u', "$1\n$2", '[ติดต่อนัดหมายชมโครงการหรือสอบถามข้อมูลติดต่อนัดัด]');
The result shows without ':
[ติดต่อนัดหมายชมโครงการหรือสอบถามข้อมูลติดต่อนัดัด\n
]
Why does it add additional ' in the new line ?
How can I avoid it ?

Related

hide just couple of html tags

I have comment box. If i type in something like this
aa #Martins <aabb>
In database I save it like:
aa <span class="highlight" contenteditable="false">#Martins Vilskersts</span> <aabb><span></span>
And for now i use this, to show it:
$str = strip_tags(htmlspecialchars_decode(html_entity_decode($my_string_from_database)), '<br><br/>');
//here is some replace for links functionality
$replace = '<a href="javascript:;" class="..." id="..." ></a>';
$str = str_replace($link->tag, $replace, $str);
echo $str;
And i get result like this:
aa #Martins
But i want to see it like this:
aa #Martins <aabb> -[with # functionality, but with some random <aaa><bbb> tags as plain text. Any idea?]
USE THIS:
just replace < by < and > by >
Keep this in mid as well :
'&' (ampersand) becomes &
'"' (double quote) becomes " when ENT_NOQUOTES is not set.
"'" (single quote) becomes ' only when ENT_QUOTES is set.
'<' (less than) becomes <
'>' (greater than) becomes >
If you just literally output the string as stored in the database, without using htmlspecialchars_decode, strip_tags, html_entity_encode, etc, then it'll come out properly.
You have already saved the parts you want to see as encoded characters, and the parts that should work as raw html in your database.

PHP replace : find and replace the same characters with different text

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 );

Find the count of occurrences of a substring within a string in php

I have a string say
$result = \nHey there!Listen\n\n===\n\nLife is one\n\n===\n\nBut cs is 1.6ok\n\n===\n\nI hope you got what i wanna say\n\nSpeech finished\n\n===\n
Now I want to
Find the no of occurences of \n in the above string. I have used the below function but it outputs nothing :
substr_count($result,"\n");
I want to email $result as the Email-body , but \n characters are appearining as it is.
Anyway out there, that I can get rid of these \n characters and they are actually replaced by a new line in email subject ?
$result = str_replace("\n", chr(13), $result );
I replaced all the \n characters by chr(13) that is interpreted as enter .
So it worked out for me :)

Problems with newlines (nl2br)

My code works as follows:
Text comes to server (from textarea)
Text is ran through trim() then nl2br
But what is happening is it is adding a <br> but not removing the new line so
"
something"
becomes
"<br>
something"
which adds a double new line. Please help this error is ruining all formatting, I can give more code on request.
Creation of post:
Shortened creation method (Only showing relevent bits) Creation method:
BlogPost::Create(ParseStr($_POST['Content']));
ParseStr runs:
return nl2br(trim($Str));
Viewing of post:
echo "<span id='Content'>".BlogPosts::ParseBB(trim($StoredPost->Content))."</span>";
ParseBB runs:
$AllowedTags = array(
// i => Tag, Tag Replacement, Closing tag
0 => array("code","pre class='prettyprint'",true),
1 => array("center","span style='text-align:center;'",true),
2 => array("left","span style='text-align:right;'",true),
3 => array("right","span style='text-align:left;'",true)
);
$AllowedTagsStr = "<p><a><br><br/><b><i><u><img><h1><h2><h3><pre><hr><iframe><code><ul><li>";
$ParsedStr = $Str;
foreach($AllowedTags as $Tag)
{
$ParsedStr = str_replace("<".$Tag[0].">","<".$Tag[1].">",$ParsedStr);
if($Tag[2])
$ParsedStr = str_replace("</".$Tag[0].">","</".$Tag[1].">",$ParsedStr);
}
return strip_tags($ParsedStr,$AllowedTagsStr);
Example:
What I see:
What is shown:
It's because nl2br() doesn't remove new lines at all.
Returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).
Use str_replace instead:
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
Aren't you using UTF-8 charset? If you are using multibyte character set (ie UTF-8), trim will not work well. You must use multibyte functions. Try something like this one: http://www.php.net/manual/en/ref.mbstring.php#102141
Inside <pre> you should not need to call nl2br function to display break lines.
Check if you really want to call nl2br when you are creating post. You probably need it only on displaying it.

how to remove ♥ (or ♥) from a string in php?

this text block represents the original external source that is a variable:
// the following characters are not-consistent
text here
// the following characters are consistent
♥ text1
♥ text2
♥ text3
// the following characters are consistent
some more text here
i want to remove everything except for "text here" and i have to do this in 'parts' as other text may be present (ie i can't just remove everything after "text here").
i can remove "some more text here" but am having trouble trying to target and remove the lines beginning with ♥ (or ♥ as they are currently being stored in the database).
so these are my variables
// this is the original external source
$part_one = $external_source;
// this is the variable that is not working yet
$part_one_a = ;
// this replaces "some more text here" with nothing
$part_one_b = str_replace("some more text here", " ", $part_one);
// this concatenates the variables $part_one_a and $part_one_b
$final = $part_one_a . $part_one_b;
thank you.
update
to be more specific i need a way to remove text such as this:
♥ link_one: http://link_one_here.com/
♥ link_two: http://link_two_here.com/
♥ link_three: http://link_three_here.com/
update two
i've managed to remove all unwanted items with str_replace on an array.
the only thing i cannot target and therefore remove is the ♥.
i have tried targeting it with:
♥ (this is the value that is currently being stored in the
database)
♥ (after i applied a htmlentities to the string)
and no luck - the ♥ is still being displayed. if anyone can tell me how i can target ♥ it would be much appreciated and this question would be resolved.
thank you.
If i understand you correctly you need the all text here which is before ♥ you can use strpos
$stopper = '♥';
$string = 'text here
♥ text1
♥ text2
♥ text3
some more text here' ;
$final = trim(substr($string,0,strpos($string, $stopper)));
var_dump($final);
Output
string 'text here' (length=9)
Please note that your $stopper can also be \n new line
the solution was:
Do a str_replace on an array to target and remove specific portions of text.
Create a regex and define acceptable values.
Do a preg_replace on non-acceptable values.
this resulted in all the desired values being shown without instances of non-acceptable characters.
the code for the last two steps is below:
// this is the original external source
$part_one = $external_source;
// create a regex
$regex
= '~'
. '['
. '^'
. 'A-Z'
. '0-9'
. ' '
. '!_:/,.#$-'
. ']'
. '~'
. 'i'
;
// clean the string
$part_one_cleaned = preg_replace($regex, NULL, $part_one);

Categories