So there's my problem :
$var = "93 Avenue d'Aubière";
I used this $var with this function:
function stripAccents($str) {
$str = preg_replace('/[\x{00C0}\x{00C1}\x{00C2}\x{00C3}\x{00C4}\x{00C5}]/u','A', $str);
$str = preg_replace('/[\x{0105}\x{0104}\x{00E0}\x{00E1}\x{00E2}\x{00E3}\x{00E4}\x{00E5}]/u','a', $str);
$str = preg_replace('/[\x{00C7}\x{0106}\x{0108}\x{010A}\x{010C}]/u','C', $str);
$str = preg_replace('/[\x{00E7}\x{0107}\x{0109}\x{010B}\x{010D}}]/u','c', $str);
$str = preg_replace('/[\x{010E}\x{0110}]/u','D', $str);
$str = preg_replace('/[\x{010F}\x{0111}]/u','d', $str);
$str = preg_replace('/[\x{00C8}\x{00C9}\x{00CA}\x{00CB}\x{0112}\x{0114}\x{0116}\x{0118}\x{011A}]/u','E', $str);
$str = preg_replace('/[\x{00E8}\x{00E9}\x{00EA}\x{00EB}\x{0113}\x{0115}\x{0117}\x{0119}\x{011B}]/u','e', $str);
$str = preg_replace('/[\x{00CC}\x{00CD}\x{00CE}\x{00CF}\x{0128}\x{012A}\x{012C}\x{012E}\x{0130}]/u','I', $str);
$str = preg_replace('/[\x{00EC}\x{00ED}\x{00EE}\x{00EF}\x{0129}\x{012B}\x{012D}\x{012F}\x{0131}]/u','i', $str);
$str = preg_replace('/[\x{0142}\x{0141}\x{013E}\x{013A}]/u','l', $str);
$str = preg_replace('/[\x{00F1}\x{0148}]/u','n', $str);
$str = preg_replace('/[\x{00D2}\x{00D3}\x{00D4}\x{00D5}\x{00D6}\x{00D8}]/u','O', $str);
$str = preg_replace('/[\x{00F2}\x{00F3}\x{00F4}\x{00F5}\x{00F6}\x{00F8}]/u','o', $str);
$str = preg_replace('/[\x{0159}\x{0155}]/u','r', $str);
$str = preg_replace('/[\x{015B}\x{015A}\x{0161}]/u','s', $str);
$str = preg_replace('/[\x{00DF}]/u','ss', $str);
$str = preg_replace('/[\x{0165}]/u','t', $str);
$str = preg_replace('/[\x{00D9}\x{00DA}\x{00DB}\x{00DC}\x{016E}\x{0170}\x{0172}]/u','U', $str);
$str = preg_replace('/[\x{00F9}\x{00FA}\x{00FB}\x{00FC}\x{016F}\x{0171}\x{0173}]/u','u', $str);
$str = preg_replace('/[\x{00FD}\x{00FF}]/u','y', $str);
$str = preg_replace('/[\x{017C}\x{017A}\x{017B}\x{0179}\x{017E}]/u','z', $str);
$str = preg_replace('/[\x{00C6}]/u','AE', $str);
$str = preg_replace('/[\x{00E6}]/u','ae', $str);
$str = preg_replace('/[\x{0152}]/u','OE', $str);
$str = preg_replace('/[\x{0153}]/u','oe', $str);
$str = preg_replace('/[\x{0022}\x{0025}\x{0026}\x{0027}\x{00A1}\x{00A2}\x{00A3}\x{00A4}\x{00A5}\x{00A6}\x{00A7}\x{00A8}\x{00AA}\x{00AB}\x{00AC}\x{00AD}\x{00AE}\x{00AF}\x{00B0}\x{00B1}\x{00B2}\x{00B3}\x{00B4}\x{00B5}\x{00B6}\x{00B7}\x{00B8}\x{00BA}\x{00BB}\x{00BC}\x{00BD}\x{00BE}\x{00BF}]/u',' ', $str);
return $str;
}
This return an empty string if I use $var but it return the correct string if I use "93 Avenue d'Aubière" as a parameter.
I tried to use preg_last_error to check if there was any error but it return 0 that means no error.
I'm connectin my DB like this:
$db = new PDO('mysql:host=localhost;dbname=somedb;charset=utf8', 'username', 'password');
Getting data like this :
$sqlSelectCommandeExapaq = "SELECT * FROM commande_exapaq WHERE statut = 'en cours'";
$res = $db->query($sqlSelectCommande);
$arrayResCmd = $res->fetchAll();
Then I passed $arrayResCmd into this function :
public static function generateInterfaceFile($orders_array)
{
// Init file
$record = new DPDStation();
// Loop through each order
foreach ($orders_array as $order_data)
{
// Add data to file
$record->add($order_data['customer_adress'], 0, 35);
}
return $record;
}
And there is the DPDStation Constructor :
function __construct() {
$this->line = str_pad("", 1634);
$this->contenu_fichier = '';
}
And the add function :
function add($txt, $position, $length) {
$txt = $this->stripAccents($txt);
$this->line = substr_replace($this->line, str_pad($txt, $length), $position, $length);
}
And adding content into the file with :
$dpd = new DPDStation();
$record = $dpd->generateInterfaceFile($arrayResCmd);
file_put_contents($filename, '$VERSION=110'."\r\n", FILE_APPEND);
file_put_contents($filename, $record->contenu_fichier."\r\n", FILE_APPEND);
Because nothing was add to the file, I look into the stripAccents and the problem seems to come from it.
Thanks for your help :)
I have a csv file that contains company names. I would want to match it against my database. In order to have a cleaner and nearer matches, I am thinking of eliminating some company suffixes like 'inc', ' inc', ', inc.' or ', inc'. Here's my sample code:
$string = 'Inc Incorporated inc.';
$wordlist = array("Inc","inc."," Inc.",", Inc.",", Inc"," Inc");
foreach ($wordlist as &$word) {
$word = '/\b' . preg_quote($word, '/') . '\b/';
}
$string = preg_replace($wordlist, '', $string);
$foo = preg_replace('/\s+/', ' ', $string);
echo $foo;
My problem here is that the 'inc.' doesn't get removed. I'm guessing it has something to do with the preq_quote. But I just can't figure out how to solve this.
Try this :
$string = 'Inc incorporated inc.';
$wordlist = array("Inc","inc.");
foreach ($wordlist as $word) {
$string =str_replace($word, '', $string);
}
echo $string;
OR
$string = 'Inc Incorporated inc.';
$wordlist = array("Inc","inc.");
$string = str_replace($wordlist, '', $string);
echo $string;
This will output as 'corporated'...
If you want "Incorporated" as result, make the "I" is small.. and than run my above code (first one)...
Try this. It may involve type juggling at some point, but will have your desired result
$string = 'Inc Incorporated inc.';
$wordlist = array('Inc', 'inc.');
$string_array = explode(' ', $string);
foreach($string_array as $k => $a) {
foreach($wordlist as $b) {
if($b == $a){
unset($string_array[$k]);
}
}
$string_array = implode('', $string_array);
You can use this
$string = 'Inc Incorporated inc.';
$wordlist = array("Inc "," inc.");
$foo = str_replace($wordlist, '', $string);
echo $foo;
Run this code here
This will work for any number of elements in array...
$string = 'Inc Incorporated inc.';
$wordlist = array("Inc");
foreach($wordlist as $stripped)
$string = preg_replace("/\b". preg_quote($stripped,'/') ."(\.|\b)/i", " ", $string) ;
$foo = preg_replace('/\s+/', ' ', $string);
echo $foo;
Another problem with str_replace, I would like to change the following $title data into URL by taking the $string between number in the beginning and after dash (-)
Chicago's Public Schools - $10.3M
New Jersey - $3M
Michigan: Public Health - $1M
The desire output is:
chicago-public-school
new-jersey
michigan-public-health
PHP code I am using
$title = ucwords(strtolower(strip_tags(str_replace("1: ", "", $title))));
$x = 1;
while ($x <= 10) {
$title = ucwords(strtolower(strip_tags(str_replace("$x: ", "", $title))));
$x++;
}
$link = preg_replace('/[<>()!#?:.$%\^&=+~`*é"\']/', '', $title);
$money = str_replace(" ", "-", $link);
$link = explode(" - ", $link);
$link = preg_replace(" (\(.*?\))", "", $link[0]);
$amount = preg_replace(" (\(.*?\))", "", $link[1]);
$code_entities_match = array(''s', '"', '!', '#', '#', '$', '%', '^', '&', '*', '(', ')', '+', '{', '}', '|', ':', '"', '<', '>', '?', '[', ']', '', ';', "'", ',', '.', '_', '/', '*', '+', '~', '`', '=', ' ', '---', '--', '--');
$code_entities_replace = array('', '-', '-', '', '', '', '-', '-', '', '', '', '', '', '', '', '-', '', '', '', '', '', '', '', '', '', '-', '', '-', '-', '', '', '', '', '', '-', '-', '-', '-');
$link = str_replace($code_entities_match, $code_entities_replace, $link);
$link = strtolower($link);
Unfortunately the result I got:
-chicagoamp9s-public-school
2-new-jersey
3-michigan-public-health
Anyone has a better solution for this? Thanks guys!
(the ' changed into amp9 - wonder why?)
If I understand you correctly:
if (preg_match('!\d+:\s+(.*)\s+-\s+\$\d+(?:\.\d+)?!', $title, $groups)) {
$words = strip_tags(strtolower($groups[1]));
$words = preg_replace('\[^\s\w]!', '', $words);
$words = preg_replace('!\s+!', '-', $words);
$words = preg_replace('!-+!', '-', $words);
echo $words;
}
One thing: your text has "1. Chicago's..." not "1: ..." like your code would seem to suggest. Is one an error or is there something else going on?
You can do:
$str = "1. Chicago's Public Schools - $10.3M";
$from = array('/^\d+\.\s+([^-]*) -.*$/','/[^A-Z ]/i','/\s+/');
$to = array("$1",'','-');
$str = strtolower(preg_replace($from,$to,$str));
echo $str; // prints chicagos-public-schools
<?php
$lines = array("1. Chicago's Public Schools - $10.3M",
"2. New Jersey - $3M",
"3. Michigan: Public Health - $1M"
);
// remove the number bullets
$lines = preg_replace('/\ - \$\d*\.?\d*M$/', '', $lines);
// remove the trailing dollar amount
$lines = preg_replace('/^\d+\.\ /', '', $lines);
// remove ignore chars
$ignore_pattern = "/['s|:]/";
$lines = preg_replace($ignore_pattern, '', $lines);
for ($i=0; $i<count($lines); $i++) {
$lines[$i] = implode('-',explode(' ', strtolower(trim($lines[$i]))));
}
print_r($lines);
and the output:
Array
(
[0] => chicago-public-school
[1] => new-jerey
[2] => michigan-public-health
)
EDIT Start:
<?php
$lines = array("1. Chicago's Public Schools - $10.3M",
"2. New Jersey - $3M",
"3. Michigan: Public Health - $1M",
"4. New York's Starbucks - $2M",
);
$lines = preg_replace('/\ - \$\d*\.?\d*M$/', '', $lines);
$lines = preg_replace('/^\d+\.\ /', '', $lines);
$ignore_strings = array("'s", ':');
for ($i=0; $i<count($lines); $i++) {
foreach ($ignore_strings as $s) {
$lines[$i] = str_replace($ignore_strings, '', $lines[$i]);
}
}
for ($i=0; $i<count($lines); $i++) {
$lines[$i] = implode('-',explode(' ', strtolower(trim($lines[$i]))));
}
print_r($lines);
output:
Array
(
[0] => chicago-public-schools
[1] => new-jersey
[2] => michigan-public-health
[3] => new-york-starbucks
)
Hope it meets your needs.
EDIT End.
Assuming you already have extracted titles correctly like "Chicago's Public Schools", then to generate pagenames out of them:
function generatePagename($s) {
//to lower
$pagename = trim(html_entity_decode(strtolower($s), ENT_QUOTES));
//remove 's
$pagename = trim(preg_replace("/(\'s)/", "", $pagename));
//replace special chars with spaces
$pagename = trim(preg_replace("/[^a-z0-9\s]/", " ", $pagename));
//replace spaces with dashes
$pagename = trim(preg_replace("/\s+/", "-", $pagename));
return $pagename;
}
Which will convert something like
Chicago's "Public": Scho-ols1+23
to
chicago-public-scho-ols1-23.
Finally I looked back to initial code and have some fixes:
$title = ucwords(strtolower(strip_tags(str_replace("1. ","",$title))));
$x=1;
while($x <= 10) {
$title = ucwords(strtolower(strip_tags(str_replace("$x. ","",$title))));
$x++;
}
$data = preg_replace('/[<>()!#?:.$%\^&=+~`*&#;"\']/', '',$title);
$urldata = str_replace(" ","-",$data);
$data = explode(" - ",$data);
$link = preg_replace(" (\(.*?\))", "", $data[0]);
$budget = preg_replace(" (\(.*?\))", "", $data[1]);
$code_entities_match = array( '"' ,'!' ,'#' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'+' ,'{' ,'}' ,'|' ,':' ,'"' ,'<' ,'>' ,'?' ,'[' ,']' ,'' ,';' ,"'" ,',' ,'.' ,'_' ,'/' ,'*' ,'+' ,'~' ,'`' ,'=' ,' ' ,'---' ,'--','--');
$code_entities_replace = array('' ,'-' ,'-' ,'' ,'' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'-' ,'-' ,'-','-');
$link = str_replace($code_entities_match, $code_entities_replace, $link);
$link = strip_tags(str_replace("amp39s","",$link));
$link = strtolower($link);
What a mess, I know, but it works anyway, thanks guys for helping me, especially cletus who found the mistake between 1. and 1:
I have my class
public function convert( $title )
{
$nameout = strtolower( $title );
$nameout = str_replace(' ', '-', $nameout );
$nameout = str_replace('.', '', $nameout);
$nameout = str_replace('æ', 'ae', $nameout);
$nameout = str_replace('ø', 'oe', $nameout);
$nameout = str_replace('å', 'aa', $nameout);
$nameout = str_replace('(', '', $nameout);
$nameout = str_replace(')', '', $nameout);
$nameout = preg_replace("[^a-z0-9-]", "", $nameout);
return $nameout;
}
BUt I can't get it to work when I use special characters like ö and ü and other, can sombody help me here? I use PHP 5.3.
The first answer in this SO thread contains the code you need to do this.
And what about:
<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>
From: http://php.net/manual/en/function.urlencode.php
I wrote this function a while ago for a project I was working on and couldn't get RegEx to work. Its not the best way, but it works.
function safeURL($input){
$input = strtolower($input);
for($i = 0; $i < strlen($input); $i++){
$working = ord(substr($input,$i,1));
if(($working>=97)&&($working<=122)){
//a-z
$out = $out . chr($working);
} elseif(($working>=48)&&($working<=57)){
//0-9
$out = $out . chr($working);
} elseif($working==46){
//.
$out = $out . chr($working);
} elseif($working==45){
//-
$out = $out . chr($working);
}
}
return $out;
}
Here's a function to help with what you're doing, it's written in
Czech: http://php.vrana.cz/vytvoreni-pratelskeho-url.php
(and translated to English)
Here's another take on it (from the Symfony documentation):
<?php
function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
if (function_exists('iconv'))
{
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
}
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text))
{
return 'n-a';
}
return $text;
}