1

I am working on youtube video feed, and I want to transform each video title to seotitle.

I have a string like so: 'Kings Of Leon - Use Somebody'

and I want it to be like this: 'Kings-Of-Leon-Use-Somebody'.

I have try this code:

$seotitle=str_replace(' ','-',$video['title']['$t']);

but I get this 'Kings-Of-Leon-Use---Somebody'

What is a good way to handle with extra spaces and minus?

krishna
  • 4,069
  • 2
  • 29
  • 56
user2706762
  • 397
  • 1
  • 3
  • 11
  • Use a regular expression function, e.g. `preg_replace` - you can use the `+` device to specify a number of characters. Have a look at php.net/preg_replace for more info. – halfer Apr 02 '14 at 12:09
  • http://www.punctuationmatters.com/the-difference-between-a-dash-and-a-minus-sign/ – Thomas Orlita Aug 09 '17 at 16:32

4 Answers4

4

A simple regex to do what you want is:

preg_replace('|[ -]+|', '-', $seotitle);
HamZa
  • 14,671
  • 11
  • 54
  • 75
Dragony
  • 1,712
  • 11
  • 20
1

Try this one:

$string = 'Kings Of Leon - Use Somebody';

// first replace anything but letters with space
$string = preg_replace('/[^a-zA-Z]/', ' ', $string);

// then replace consecutive spaces with hypen
$string = preg_replace('/[ ]+/', '-', trim($string));

See inline comments for detail.

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
1

Try This :

//string assign to variable
$string = 'Kings Of Leon - Use Somebody';
//Removes whitespace or other predefined characters from the right side of a string
$string = ltrim($string);
// Removes whitespace or other predefined characters from both sides of a string
$string = rtrim($string);
// str replace methods
$string = str_replace(" ", "-", $string);
// RemoveSpecial Character
$string = preg_replace("/[^A-Za-z0-9\-]/", "",$string);
// Remove Multiple -
$string = preg_replace('/-+/', '-',$string);
Ravi Delixan
  • 2,584
  • 1
  • 22
  • 31
-1

You can use the same function to replace the ' - ' with ' ' like this:

$seotitle_temp=str_replace(' - ', ' ', 'Kings Of Leon - Use Somebody'); 
$seotitle_temp=str_replace(' ', '-', 'Kings Of Leon - Use Somebody');

Then you will go clear.

Alex7
  • 560
  • 3
  • 19
  • This will not have any _-_ in the title. – putvande Apr 02 '14 at 12:20
  • This will replace the line here 'Kings Of Leon - Use Somebody' and then replace spaces with '-' and he gets 'Kings-Of-Leon-Use-Somebody'. Try it. – Alex7 Apr 02 '14 at 12:22
  • I get the title with just spaces and no dashes. – putvande Apr 02 '14 at 12:28
  • The replace the spaces with dashes. – Alex7 Apr 02 '14 at 12:58
  • No, it replaces the one dash in the string with a space. `str_replace(' - ', ' ', 'Kings Of Leon - Use Somebody');` becomes _Kings Of Leon Use Somebody_ – putvande Apr 02 '14 at 14:14
  • @putvande `str_replace(' - ', ' ', 'Kings Of Leon - Use Somebody');` `str_replace(' ', '-', 'Kings Of Leon - Use Somebody');` Execute this. – Alex7 Apr 02 '14 at 14:45
  • If you make a variable out of it and put it trough the 2 str_replace it works. So put that as your answer. – putvande Apr 02 '14 at 15:04