How to replace backslash and white space from string - php

How can I replace "/" and " " from the given string "one size x/l" and generate output like "one-size-x-l" using preg_replace.

Using Regular expression you can replace all the special characters by "-" as below:
$str= "one size x/l";
$str= preg_replace("![^a-z0-9]+!i", "-", $str);
Hope this helps:)

Use the below function, which will make fnie urls as required
function seoUrlAscii($str, $replace=array(), $delimiter='-') {
if( !empty($replace) ) { $str = str_replace((array)$replace, ' ', $str); }
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}

$a="one size x/l";
echo preg_replace('(/|\s)','-', $a);
got my output as one-size-x-l

Related

Generate slug with german umlauts

I try to generate a slug from a string, but I got some problems with the german umlauts:
$text = 'Ein schöner Text';
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
$text = trim($text, '-');
$text = iconv('utf-8', 'ASCII//TRANSLIT', $text);
$text = strtolower($text);
$text = preg_replace('~[^-\w]+~', '', $text);
The result should be: 'ein-schoener-text'
Change the second preg_replace line to the below because to match any letter in any language you need to use \p{L} pattern.
$text = preg_replace('~[^\p{L}\d]+~u', '-', $text);
Code:
<?php
$text = 'Ein schöner Text';
$text = preg_replace('~[^\p{L}\d]+~u', '-', $text);
$text = trim($text, '-');
$text = iconv('utf-8', 'ASCII//TRANSLIT', $text);
$text = strtolower($text);
$text = preg_replace('~[^-\w]+~', '', $text);
echo $text;
?>
Output:
ein-schoner-text

Is there a php function to convert these characters?

I am using a lot of str_replace such as:
$prune = str_replace("'", '\'', $prune);
$prune = str_replace('’', '\'', $prune);
$prune = str_replace('–', '-', $prune);
$prune = str_replace('“', '', $prune);
$prune = str_replace('"', '', $prune);
$prune = str_replace('&#233', 'e', $prune);
$prune = str_replace('&', '&', $prune);
Is there a simpler PHP function for this, such as htmlspecialchars_decode()?
You are looking for html_entity_decode; it mirrors htmlentities as htmlspecialchars_decode mirrors htmlspecialchars.
You could use arrays in str_replace
$search = array("'", '’', '–', '“', '"', '&#233', '&');
$replace = array('\'', '\'', '-', '', '', 'e', '&');
$prune = str_replace($search, $replace, $prune);
Try $prune = htmlspecialchars_decode($prune);
Click http://us1.php.net/manual/en/function.htmlspecialchars-decode.php

Propper Regex Pattern

I need to check the incoming string and leave only characters, matching:
small case a-z letters
_ character
any numbers
only one dot (first one)
$string = 'contDADdas7.6.asdASDj_##e1!Ddd__aa#S.txt';
$pattern = "/[a-z_0-9]+/";
preg_match_all("/[a-z_0-9]+/", $name, $result);
echo implode('', $result[0]);
has to be
contdas7.6asdj_e1dd__aatxt
It matches first three points, how can I take only one first dot ?
You can try this:
$string = strrev($string);
$string = preg_replace('~[^a-z0-9_.]++|\.(?![^.]*$)~', '', $string);
$string = strrev($string);
An other way:
$strs = explode('.', $string);
if (count($strs)>1) {
$strs[0] .= '.' . $strs[1];
unset($strs[1]);
}
$string = preg_replace('~[^a-z0-9_.]++~', '', implode('', $strs));
<?php
$str = "contDADdas7.6.asdASDj_##e1!Ddd__aa#S.txt";
preg_match_all("/[a-z_0-9\.]+/", $str, $match);
$newstr = implode("", $match[0]);
echo substr_replace(str_replace(".", "", $newstr), ".", strpos($newstr, "."), 0);
Output:
contdas7.6asdj_e1dd__aatxt

PHP slugifying HTML entity code backwards R

I'm trying to create some clean URLs for artist names. I'm using the following to parse Pearl Jam to pearl-jam. All is fine until it tries to process KoЯn. Instead of returning korn, it is returning koand1071n. In my database, KoЯn is actually stored as
KoЯn
I'm using this code below. What can I do to handle the backwards R?
$delimiter = "-";
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = str_replace("\$", "s", $clean);
$clean = preg_replace( '/&/', 'and', $clean );
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
$clean = strtolower(trim($clean, '-'));
Just replace Я with a r with str_replace like you do for $
$clean = str_replace("Я", "r", $clean);
or
$clean = str_replace("Я", "r", $clean);

Replace empty spaces with ### from Text between " " in a string in PHP

I have a string like this one text more text "empty space".
How can I replace the space in "empty space" and only this space with ###?
$string = 'text more text "empty space"';
$search = 'empty space';
str_replace($search, 'empty###space', $string);
How about this, with no regular expressions:
$text = 'foo bar "baz quux"';
$parts = explode('"', $text);
$inQuote = false;
foreach ($parts as &$part) {
if ($inQuote) { $part = str_replace(' ', '###', $part); }
$inQuote = !$inQuote;
}
$parsed = implode('"', $parts);
echo $parsed;
$somevar = "empty space";
$pattern = "/\s/";
$replacement = "###";
$somevar2 = preg_replace($pattern, $replacement, $somevar);
echo $somevar2;
$string = "My String is great";
$replace = " ";
$replace_with = "###";
$new_string = str_replace($replace, $replace_with, $string);
This should do it for you. http://www.php.net/manual/en/function.str-replace.php
Edited after you comments
Maybe it's not the best solution, but you can do it like this:
$string = 'text more text "empty space"';
preg_match('/(.*)(".*?")$/', $string, $matches);
$finaltext = $matches[1] . str_replace(' ', '###', $matches[2]);

Categories