How to remove whitespace + hiphen + whitespace to only hiphen in url slug with preg_replace - preg-replace

I am using this function.
Everything works fine but When i enter item title like : "Example - A digital product" it shows me in url slug example---a-digital-product..Here after word example there are 3 hiphen continuous. Please help to resolve it.
public function item_slug($string){
$slug=preg_replace(array('/[^A-Za-z0-9 -]+/','/[ -]+/'), array('',''),$string);
return $slug;
}

You could use a hyphen in the second replacement to replace 1 or more occurrences of a space or hyphen you match with [ -]+ with a single hyphen.
Example code:
function item_slug($string){
return preg_replace(array('/[^A-Za-z0-9 -]+/','/[ -]+/'), array('','-'),$string);
}
$strings = [
"example---a-digital-product..",
"Example - A digital product"
];
foreach ($strings as $str) {
echo item_slug($str) . PHP_EOL;
}
Output
example-a-digital-product
Example-A-digital-product
Php demo

Related

PHP Remove specific line return

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)

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 Remove All Non-alphanumeric Characters Except Apostrophes

$keywords = explode("\n",$_POST['keywords']);
//Inserting into Database
foreach($keywords as $key => $keyword){
$keyword = strtolower(trim($keyword));
$keyword = preg_replace("/[^A-Za-z0-9' ]/", "", $keyword);
$keyword = preg_replace("/\s+/", " ", $keyword);
$insertkeywords = "INSERT IGNORE INTO kwdb (keyword) VALUES ('$keyword')";
mysql_query($insertkeywords);
}
I can't for the life of me figure out why this code won't insert the keywords into the database when I have an apostrophe in the:
"/[^A-Za-z0-9' ]/"
But when I remove it to be:
"/[^A-Za-z0-9 ]/"
This script inserts records into the database.
What am I trying to do?
I have a textarea on a form. For each keyword on a new line, I explode based on the \n. I want to remove all non-letter and non-number characters from the keywords but don't want apostrophes to be removed.
I basically want all keywords to be lower-case with leading, trailing and extra white spaces - 2 or more blank spaces trimmed down to one - removed along with any non-letter and non-number characters except for apostrophes.
You need to escape your keyword: $insertkeywords = "INSERT IGNORE INTO kwdb (keyword) VALUES ('".mysql_real_escape_string($keyword)."')";
Thank you clover. Without your help, it would have taken me many hours to figure that one out! I ended up using mysqli_real_escape_string instead of mysql_real_escape_string because mysql_real_escape_string says it is deprecated as of PHP 5.5.0. As a result, I had to change it up a bit but the below code seems to work great for me so far (in case anyone else runs across this).
$con = mysqli_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
else
{
echo "Connected Successfully!";
}
//select database
mysqli_select_db($con, "test");
$keywords = explode("\n",$_POST['keywords']);
//Inserting into Database
foreach($keywords as $key => $keyword){
$keyword = preg_replace("/[^A-Za-z0-9' ]/", " ", $keyword);
$keyword = preg_replace("/\s+/", " ", $keyword);
$keyword = strtolower(trim($keyword));
//$insertkeywords = "INSERT IGNORE INTO kwdb (keyword) VALUES ('$keyword')";
$insertkeywords = "INSERT IGNORE INTO kwdb (keyword) VALUES ('".mysqli_real_escape_string($con, $keyword)."')";
mysqli_query($con, $insertkeywords);
}

Using Parse::Lex, is there a way to return tokens only in certain states/conditions

Assuming that i need to tokenize and parse only multiline comments, how will i do that using Parse::Lex. When using flex-bison, the default action for any pattern in the rules section of the lex file used to be 'skip'.
%%
.* ;
%%
How to do this here ?
[EDIT] Well, i tried that, i'm still missing something - here is my code - and result. Where have i gone wrong ??
my simplified lex file:
use Parse::Lex;
use Regexp::Common;
use YParser;
my $lexer;
my #token = (
qw|esp:TA abcdefgh|,
qw(esp:REST .|\n),
);
Parse::Lex->trace;
Parse::Lex->exclusive('esp');
$lexer = Parse::Lex->new(#token);
$lexer->from(\*STDIN);
$lexer->skip(qr! [ \t]+ | $RE{balanced}{-begin=>'/*'}{-end=>'*/'} !xms);
$lexer->start('esp');
my $j = YParser->new();
$j->YYParse(yylex => \&lex);
sub lex {
my $token = $lexer->next;
return ('', undef) if $lexer->eoi;
if ($token->name eq 'TA' || $token->name eq 'REST') {
return ($token->name, {LINENO => $lexer->line, TEXT => $token->text});
}
}
my simplified grammar file
% token TA REST
%%
Program: Element
| Program Element
;
Element: TA
| REST
;
%%
Input file:
abcdefgh
/*sdf*/
Result:
perl lexfile.pl < inputfile
Trace is ON in class Parse::Lex
Can't call method "name" on an undefined value at qnlex.pl line 26, <STDIN> line 1.
Use the skip setting, shown here using Regexp::Common to help construct a regexp matching balanced pairs of comment delimiters. I've assumed /* */ as the comment delimiters, but they could be anything.
$lexer->skip(qr! [ \t]+ | $RE{balanced}{-begin=>'/*'}{-end=>'*/'} !xms);
The [ \t]+ alternative is left in place since that's the default.
Well, i figured this out :) Very simple - all i have to do is make the lex get the next token when encountering tokens i want to skip. Below is code to skip passing the token 'REST' to the parser.
sub lex {
my $token;
NEXTTOKEN:
$token = $lexer->next;
return ('', undef) if $lexer->eoi;
if ($token->name eq 'TA') {
return ($token->name, {LINENO => $lexer->line, TEXT => $token->text});
}
elsif ($token->name eq 'REST') {
goto NEXTTOKEN;
}
}

Double Quote Problem after Saving an INI file with Zend_Config

I got a problem when I want to write and save an INI file. I use Zend_Config_Ini to handle this procedure.
The problem is I always got 'Double Quote' in the value for every line that use integer or number afer saving process. Here is the example
The original application.ini file
resources.session.use_only_cookies = 1
resources.session.remember_me_seconds = 86400
After I run these lines of code
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini',
null,
array('skipExtends' => true,
'allowModifications' => true));
// Modify a value
$config->production->resources->db->adapter = 'foobar';
// Write the config file
$writer = new Zend_Config_Writer_Ini(array('config' => $config,
'filename' => APPLICATION_PATH . '/configs/application.ini'));
$writer->write();
The application.ini lines become
resources.session.use_only_cookies = "1" //double quote appears T_T
resources.session.remember_me_seconds = "86400" //double quote appears T_T
What I want is the integer value must still remain the same (without double quotes).
Anyone can help me to solve this problem?
Thank you very much in advance
As Phil-Brown notes, when reading an ini file in PHP using parse_ini_file(), you always get strings back. Also, for for any value that isn't alphanumeric characters only, you should encase in double quotes, so Zend_Config_Writer encases all values.
Anyway,
In my solution, I had to remove some lines of code in Zend. The file that I changed was \Zend\Config\Writer\Ini.php for method _prepareValue() in line 150 to be like below
protected function _prepareValue($value)
{
/*
I comment it
if (is_integer($value) || is_float($value)) {
return $value;
} elseif (is_bool($value)) {
return ($value ? 'true' : 'false');
} else {
return '"' . addslashes($value) . '"';
}*/
return $value; // my line
}
just comment the original code of Zend and just pass and return $value.
So, by changing this file I never get any double quote anymore for string, boolean or even number. This is that what I want. :)
Thank you everyone.