tags:

views:

28

answers:

3

How can i strip out the code after the "-" (including the -) in this title using php?

Buying a Home - Conveyancing Solicitors Dorset, Devon & Cornwall - LCS Legal Services
+1  A: 

try strtok function

$title = strtok($title, '-');

alternatively you can use explode function and grab required part.

$parts = explode('-', $title);
$title = current($parts); //first
$title = end($parts); //last
$title = $parts[0] //using index
Nazariy
+1  A: 
$str = 'Buying a Home - Conveyancing Solicitors Dorset, Devon & Cornwall - LCS Legal Services
';

// find first occurrence of '-'
$pos = strpos($str, '-');

// if '-' found, take the substring from the beginning to the position found
$str = ($pos !== false) ? substr($str, 0, $pos) : $str;
cballou
this works perfectly
Andy
strtok is right function for this task
Nazariy
A: 

use explode on "-" and get first element.

$str = "Buying a Home - Conveyancing Solicitors Dorset, Devon & Cornwall - LCS Legal Services";
$s = explode("-",$str);
print $s[0];
ghostdog74