In my script I replaced all "," commas with quotation+spaces.
But when it comes to numbers which are like 3,456,778, it also converts the commas to quote+space. Is there any way to add to command to ignore big numbers like that so it doesn't convert it to:
3" 456" 778"
If there is quotationm+space+any number then convert quotation+space to comma.. I mean i know how to do it with str_replace command but i dont know how to select anynumber 0-9.
Any help to do it? To convert it to:
3,456,778
I think i need to elaborate some. I needed convert this text:
Value=3,456,778,id=777
To:
Value=3,456,778" id=777"
But problem is it also convert those middle commas in between numbers.
So even if I can change my str_replace command to this like
"If comma is not in between two numbers then only convert comma to quotation+space". It would be good. Is it possible?
What about this?
preg_replace("/,([^0-9]|$)/", "\"$1", $text);
This will match all the text except commas followed by numbers.
For instance, this:
$text = "123,23 adas , asdsa d, asdasd sa 1234,234324,asdas 324324 234,";
echo $text; echo "<br/>";
echo preg_replace("/,([^0-9]|$)/", "\"$1", $text);
Will echo this:
123,23 adas , asdsa d, asdasd sa 1234,234324,asdas 324324 234"
123,23 adas " asdsa d" asdasd sa 1234,234324"asdas 324324 234"
It is not really clear from your description what you actually want to do.
This might be a step into the right direction, however:
preg_replace('/([0-9]+)" /', '\\1,', '3" 456" 778"');
Not the best solution maybe,but can give it a try.
$copy_date = '3" 456" 778"';
$copy_date = preg_replace("(\"\s{1})", ",", $copy_date);
$copy_date1 = preg_replace("(\")", "", $copy_date);
print $copy_date1;
o/p:3,456,778
Related
I want to change Text1 to Text2.
Text1
Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.
Text2
Test1 is here<br><br>Now comes Test2<br><br>Then test 3<br><br><br>Thats it.
i.e; add extra 'breakline' tag to the existing one in a string.
I tried it with preg_replace but can't figure it the way I wanted.
My Try -
preg_replace('/(?:(?:<br>)\s*)/s', "<br><br>", $posttext)
This should do it:
$text = preg_replace('/((<br>(\s+)?)+)/', '$1<br>', $text);
If you don't want to allow for newlines and spaces try: /((<br>)+)/
Try this:
preg_replace('/((?:<br>)+)\s*/s', "$1<br>", $posttext);
This captures a sequence of <br> tags, optionally followed by whitespace, and then adds one more after them.
DEMO
try this.
$text1 = "Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.";
$text2 = substr($text1,0,strripos($text1,"<br>")) ."<br>" . substr($text1,strripos($text1,"<br>"));
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 need to remove string inside [...] including "[]" itself. I tried searching for a solution from this site. I have a clue that I should try something with preg_replace but it seems too expert to me.
For example :
[gallery ids="92,93,94,95,96,97,98,99,100" orderby="rand"] Description The thirty-two storey resort condominium in Phuket, located Just 150m from Patong beach where choices of activities, water sp
I need to remove [gallery ids="92,93,94,95,96,97,98,99,100" orderby="rand"] from the example text. And it always begins with [gallery ids=" .
Please suggest.
Try this:
$string = '[gallery ids="92,93,94,95,96,97,98,99,100" orderby="rand"] Description The thirty-two storey resort condominium in Phuket, located Just 150m from Patong beach where choices of activities, water sp ';
$string = preg_replace('/\[gallery ids=[^\]]+\]/', '', $string);
Breakdown:
\[gallery ids= look for substring that begins with [gallery ids=
[^\]]+\] match 1 or more characters that are not ] until you reach a ]
Will then replace that whole matched portion with '' nothing and you have your new string.
Hi I have the following situation where I have tags wrapped around my contents and the following input
[hello]
Text here
[/hello]
[hello]
Text here 2
[/hello]
The output is
[hello]\n\nText here\n\n[/hello]\n\n[hello]\n\nText here 2\n\n[/hello]
The desired output should be
[hello]\nText here\n[/hello][hello]\nText here 2\n[/hello]
PS: THanks for the answer, but since its user input, there's a chance of spacing [hello] \n
Is there a way using php, to trim the first \n around the opening tag ([hello]) and closing tag([/hello])? Thanks
it's simple, in the other words you want to replace \n\n to \n
$str = "[hello]\n\nText here\n\n[/hello]\n\n[hello]\n\nText here 2\n\n[/hello]";
echo str_replace("\n\n", "\n", $str);
it will output
[hello]
Text here
[/hello]
[hello]
Text here 2
[/hello]
Its simple, Try This:
$str = "[hello]\n\nText here\n\n[/hello]\n\n[hello]\n\nText here 2\n\n[/hello]";
echo str_replace("[hello]\n","[hello]", str_replace("\n[/hello]","[/hello]",$str));
Provinces is a group_concat of all the individual records that contain province, some of which are blank.
So, when I encode:
$provinces = ($row['provinces']);
echo "<td>".wordwrap($provinces, 35, "<br />")."</td>";
This is what the result looks like:
Minas Gerais,,,Rio Grande do
Sul,Santa Catarina,Paraná,São Paulo
However, when I try to preg_replace out some of the nulls, and add some spaces with this expression:
$provinces = preg_replace($patterns,
$replaces, ($row['provinces']));
echo "<td>".wordwrap($provinces, 35, "<br />")."</td>";`
This is what I get!!! :(
Minas Gerais, Rio Grande do
Sul, Santa
Catarina, Paraná, São Paulo
The output is very unnatural looking.
BTW: Here are the search and replace arrays:
$patterns[0] = '/,,([,]+)?/'; $replaces[0] = ', ';
$patterns[1] = '/^,/'; $replaces[1] = '';
$patterns[2] = '/,$/'; $replaces[2] = '';
$patterns[3] = '/\b,\b/'; $replaces[3] = ', ';
$patterns[4] = '/\s,/'; $replaces[4] = ', ';
UPDATE: I even tried to change Paraná to Parana
Minas Gerais, Rio Grande do
Sul, Santa
Catarina, Parana, São
Paulo
Don't use as the replacement. wordwrap() considers that 6 characters. It doesn't interpret the HTML entity. That's why your lines are breaking funny. If you want replace spaces after you wordwrap()
Also, your first pattern should be:
// match one or more commas together
$patterns[0] = '/,+/';
Is the wordwrap() really necessary? It sounds like you are rendering this content into a table cell of some fixed width and you don't want individual entries to split across lines.
If this inference is correct - and if none of your entries is actually so long that forcing it to a single line will break your layout - then how about this: explode() on commas into an array, remove the whitespace-only entries, replace normal spaces in each array entry with , and implode() back on , (a comma followed by a space). Then let the rendering browser break lines wherever it needs.