PHP Remove specific line return - tags

After i've done a preg_replace to remove the TAG 'TagToremove', i still have a line return, would you know if either i could remove it during the preg_replace
Or After and how ?
Thanks
<?php
$strip_list = array('TagToremove');
foreach ($strip_list as $tag)
{
$temp = preg_replace('/<\/?' . $tag . '(.|\s)*?>/', '', $temp);
}
?>
String before Preg_replace:
<CodeServiceTransport></CodeServiceTransport>
<PrixTotalCommande>100</PrixTotalCommande>
<TagToremove>
<Ligne>
<ll>hh</ll>
<Id>48</Id>
<SKU>autreID</SKU>
<Quantity>1</Quantity>
</Ligne>
</TagToremove>
<Meta-CodeActivite></Meta-CodeActivite>
<Meta-CodeEnseigne></Meta-CodeEnseigne>
String after Preg_replace:
<CodeServiceTransport></CodeServiceTransport>
<PrixTotalCommande>100</PrixTotalCommande>
<Ligne>
<ll>hh</ll>
<Id>48</Id>
<SKU>autreID</SKU>
<Quantity>1</Quantity>
</Ligne>
<Meta-CodeActivite></Meta-CodeActivite>
<Meta-CodeEnseigne></Meta-CodeEnseigne>

Don't parse HTML with regex, use a DOM parser instead.
Nevertheless, for your specific problem, you have to add an optional linebreak in your regex like that:
$temp = preg_replace('~</?' . $tag . '[^>]*>\R?~i', '', $temp);
\R stands for any kind of linebreak (ie. \r or \n or \r\n)

Related

Why Laravel Request object is replacing spaces with underscores on my form names?

I have a Form posting variables containing spaces in their names
e.g.
I perform my ajax request and i can see in chrome inspector that name is correctly passed "with blank space)
In my api.php:
Route::post('/user', 'UserController#get');
UserController
function get(Request $request)
{
dd($request->input('Name Surname')); //display null
dd($request->all()); //I notice the key's changed to Name_Surname
}
Taken that I can't change the names because they have to contain spaces (bad practice? ok but it has to be like that):
how can I avoid spaces to be replaced?
(maybe without to have to manipulate the request->all() returned array keys by hand....)
Short answer I don't believe there to be such a way.
You can map the response with a bit of string replace though:
$data = $request->all()->mapWithKeys(function($item, $key) {
return [str_replace("_", " ", $key) => $item];
});
If it's something you want to apply across the board, you could possible rig up some middleware to apply it to all requests.
If previous answer not work for you, try this:
$data = collect($request->all())->mapWithKeys(function($item, $key) {
return [str_replace("_", " ", $key) => $item];
})->toArray();
You may also normalize the Input Name if it is known...
$field_name = 'FIELD NAME WITH SPACES';
$value = request( str_replace( ' ', '_', $field_name ) );

php regex to replace last dot

http://i.imgur.com/KKtAU8X.jpg
preg_replace to change be like
http://i.imgur.com/KKtAU8Xt.jpg
replace last dot to t.
try code
/\.([^.]*)$/ or /(.*)\.([^.]*)$/
add it remove .jpg too!
I need to keep .jpg
$ cat test.php
<?
$url = 'http://i.imgur.com/KKtAU8X.jpg';
$newUrl = preg_replace('#^(.+)\.([\w]+)$#i', '$1t.$2', $url);
var_dump($url, $newUrl);
$ php test.php
string(30) "http://i.imgur.com/KKtAU8X.jpg"
string(31) "http://i.imgur.com/KKtAU8Xt.jpg"

How can I fix garbled multibyte text when using Text::vCard's as_string() method?

When using multibyte UTF-8 characters in a NOTE node, characters are garbled/lost around the newline.
For example:
$vcard = $address_book->add_vcard();
$vcard->version('3.0');
$vcard->FN('Tèśt Ûšér');
$vcard->NOTE('①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳');
say $vcard->as_string();
Produces:
BEGIN:VCARD
VERSION:3.0
FN:Tèśt Ûšér
NOTE:①②③④⑤⑥⑦⑧⑨⑩⑪��
�⑬⑭⑮⑯⑰⑱⑲⑳①②③④
⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯�
��⑱⑲⑳①②③④⑤⑥⑦⑧��
�⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳①
②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬�
��⑮⑯⑰⑱⑲⑳
END:VCARD
How would go about fixing this? I also posted this as an issue on the text-vcard project page. I think this is related to how the new lines are inserted (by inserting the raw bytes: \x0D\x0A), but I'm not sure.
It looks like the culprit is Text::vCard::Node->_wrap_utf8(). I was able to at least get it to stop cutting up characters by bypassing that method all together.
sub _wrap_utf8 {
my ( $self, $key, $value, $max, $newline ) = #_;
#bypass wrapping
return $key . $value;
…
}

inserting new line with OpenOffice::OODoc

I am having quite the issue creating a new line with this module and feel like I am just missing something.
my perl code looks like this:
use OpenOffice::OODoc;
my $name = "foo <br> bar";
$name=~s/<br>/\n/g;
my $outdir = "template.odt";
my $doc = ooDocument(file => $outdir);
my #pars = $doc->getParagraphList();
for my $p (#pars)
{
$doc->substituteText($p,'{TODAY}',$date);
$doc->substituteText($p,'{NAME}',$name);
...
Problem is when I open it in word or open office I have no newlines. Although if it open it in a text edit I have my new lines.. Any ideas of how to fix this?
Ok I figured it out, hopefully this will save someone hours of searching for the same thing. I added:
use Encode qw(encode);
ooLocalEncoding('utf8');
my $linebreak = encode('utf-8', "\x{2028}");
$doc->substituteText($p,'<br>', $linebreak);
So my final code looks like this:
use OpenOffice::OODoc;
use Encode qw(encode);
ooLocalEncoding('utf8');
my $linebreak = encode('utf-8', "\x{2028}");
my $outdir = "template.odt";
my $name = "foo <br> bar";
my $outdir = "template.odt";
my $doc = ooDocument(file => $outdir);
my #pars = $doc->getParagraphList();
for my $p (#pars)
{
$doc->substituteText($p,'{TODAY}',$date);
$doc->substituteText($p,'{NAME}',$name);
$doc->substituteText($p,'<br>', $linebreak);
...
Maybe not the best way to do things but it worked!
You could try and insert and empty para after the current one:
If the 'text' option is empty, calling this method is the equivalent
of adding a line feed.
This sequence (in a text document) inserts a linefeed immediately after paragraph 4. Replace 4 with current position.
$doc->insertElement
(
'//text:p', 4, 'text:p',
position => 'after',
text => '',
);

PHP preg_replace with any variation of upper/lowercase?

I needed to write a custom module in drupal to help out with my location search. Initially I simply needed to remove a comma from queries, and then I realized that I would need to replace all instances of states with their abbreviation (California -> CA) because of how information is stored in my database. However, upon doing this I found out that my method of using preg_replace seems to be dependent on upper/lowercase. So in this line:
$form_state['values'] = preg_replace("/alabama/", 'al', $form_state['values']);
"alabama" will be replaced with "al", but "Alabama" or "ALABAMA" will not. Is there a way to replace any instance of Alabama with its abbreviation without accounting for every possible variation in casings?
you can try also str_ireplace() it's Case-insensitive
<?php
$str = 'alabama ,Alabama,ALABAMA';
$replace = str_ireplace('alabama','al',$str);
echo $str;
echo "<br/>";
echo $test;
?>
$form_state['values'] = preg_replace("/alabama/i", 'al', $form_state['values']);
The 'i' modifier will make the pattern case-insensitive.