Hi
Can someone please tell me the best possible way to find the delimiter from a given line (not including the spaces)?
For our convenience we can assume use the email address to split if necessary. So the very next char (except space) after the email can be our delimiter. But there may be cases where the email address is at last and no delimiter are there.
Some examples are:
Ex 1:
[CODE]
jon, doe, [email protected], 996655
Ex 2:
[CODE]
[email protected]; doe; ;996655
Ex 3:
[CODE]
jon# doe# 996655# [email protected]
Ex 4:
[CODE]
jon doe 96655
Ex 5:
[CODE]
jon doe 996655 [email protected]
Ex 6:
[CODE]
jon;doe;[email protected];996655;
In ex 4 and 5 above, it should return as no delimiter found.
Any help is appreciated.
Thanks
<i>
</i>foo.bar#example.com#"I like using ""#"" or ""."" as a CSV delimiter."
So what would the expected response be for:
<i>
</i>foo.bar#example.com#"I like using ""#"" or ""."" as a CSV delimiter."
?[/QUOTE]
Hi Thanks for ur reply.
When I saved that line of yours and opened in my spreadsheet program specifying the delimiter as #, it spitted up the line very nicely. Chk screenshot.
http://i.imgur.com/K8uEBlC.png
How are they doing it?[/QUOTE]
[code=php]
<?php
function getDelimiter($str, $debug = false)
{
$email = 'w[^@s]*@[^@s]+w';
$regex = '/(^|S)s*'.$email.'s*($|S)/';
if(preg_match($regex, $str, $matches)) {
if($debug) {
echo "<pre>".var_export($matches,1)."</pre>n";
}
foreach($matches as $match) {
if(strlen($match) == 1) {
return $match;
}
}
return false;
}
else {
if($debug) {
echo "<p>Nope</p>n";
}
return false;
}
}
$data = array(
'[email protected]# foo# bar',
'foo #[email protected] #bar',
'foo # bar # [email protected]'
);
foreach($data as $test) {
$delimiter = getDelimiter($test, true);
echo "<p>Delimiter for '$test' is '$delimiter'.</p>n";
}
[/code]
[CODE][email protected] John Mathew [/CODE]
[CODE][email protected];CHARIOT Tichel;[/CODE]
[code=php]
$delims = array(',', ';', '|', '#');
$delimiter = null;
foreach($delims as $delim) {
$parts = explode($delim, $text);
if(count($parts == 3) { // or whatever correct value is
$delimiter = $delim;
break;
}
}
[/code]
$str = '000020;ACTIVE;AU VIEUX CAMPEUR;;48 RUE DES ECOLES; ;75005;PARIS;president JACQUES YVES DE RORTHAYS;AF14;M.;Jean-Jacques DENUAU;06 08 16 65 62;[email protected];F004;Melle;Magali SUREDA;06 86 48 23 30;[email protected];Melle;Anne-Charlotte MICHELET;[email protected]';
<i> </i>$a = getDelimiter($str);
echo $a; //returns 5 which is incorrect. It should return ;
<i>
</i>$str = '000020;ACTIVE;AU VIEUX CAMPEUR;;48 RUE DES ECOLES; ;75005;PARIS;president JACQUES YVES DE RORTHAYS;AF14;M.;Jean-Jacques DENUAU;06 08 16 65 62;[email protected];F004;Melle;Magali SUREDA;06 86 48 23 30;[email protected];Melle;Anne-Charlotte MICHELET;[email protected]';
<i> </i>$a = getDelimiter($str);
echo $a; //returns 5 which is incorrect. It should return ;
[code=php]
<?php
/**
* Try to figure out what the delimiter is by looking for email address
* @return string (false if not found)
* @param string $str string to search
* @param bool $debug whether to output debug info (default to false)
*/
function getDelimiter($str, $debug=false)
{
static $regex = '/(^|S)s*[^()<>@,;:\".[] 00- 31][^()<>@,;:\"[] 00- 31]*@[^()<>@,;:\"[] 00- 31]*[^()<>@,;:\".[] 00- 31]+s*(S|$)/';
if(preg_match($regex, $str, $matches)) {
if($debug) {
echo "<pre>Degbug: found email:n".var_export($matches,1)."</pre>n";
}
for($ix=1; $ix<=2; $ix++) {
if(!empty($matches[$ix]) and preg_match('/W/', $matches[$ix])) {
return $matches[$ix];
}
}
}
return false;
}
// test it:
$test = array(
'000020;ACTIVE;AU VIEUX CAMPEUR;;48 RUE DES ECOLES; ;75005;PARIS;president JACQUES YVES DE RORTHAYS;AF14;M.;Jean-Jacques DENUAU;06 08 16 65 62;[email protected];F004;Melle;Magali SUREDA;06 86 48 23 30;[email protected];Melle;Anne-Charlotte MICHELET;[email protected]',
'[email protected];CHARIOT Tichel',
'jon# doe# 996655# [email protected]',
'jon doe 996655 [email protected]'
);
foreach($test as $str) {
echo "<pre>$str:n";
$result = getDelimiter($str, true);
echo var_export($result,1)."</pre>n";
}
[/code]
<i>
</i>$test = array(
"jon doe [email protected] 996655"
);
jon doe [email protected] 996655
false
<i>
</i>(^|S)s*
...and this at the end...s*(S|$)
<i>
</i>(^|[St])s*
<i>
</i>([St]|$)
static $regex = '/(^|[St])s*[^()<>@,;:". [ ] 00- 31][^()<>@,;:" [ ] 00- 31]*@[^()<>@,;:" [ ] 00- 31]*[^()<>@,;:". [ ] 00- 31]+s*([St]|$)/';
[/QUOTE]
abc[email][email protected][/email]
qrs[email][email protected][/email]
[/QUOTE]
[code=php]
static $regex = '/(^|[St]) *?[^()<>@,;:\".[] 00- 31][^()<>@,;:\"[] 00- 31]*@[^()<>@,;:\"[] 00- 31]*[^()<>@,;:\".[] 00- 31]+ *([St]|$)/';
[/code]
[CODE] function getDelimiter($str, $debug = false)
{
static $regex = '/(^|[St]) *?[^()<>@,;:\".[] 00- 31][^()<>@,;:\"[] 00- 31]*@[^()<>@,;:\"[] 00- 31]*[^()<>@,;:\".[] 00- 31]+ *([St]|$)/';
if(preg_match($regex, $str, $matches)) {
if($debug) {
echo "<pre>Degbug: found email:n".var_export($matches,1)."</pre>n";
}
for($ix=1; $ix<=2; $ix++) {
if(!empty($matches[$ix]) and preg_match('/W/', $matches[$ix])) {
$delimiter = $matches[$ix];
$delimiter = ord($delimiter)==9 ? 'TAB' : $delimiter;
return $delimiter;
}
}
}
return false;
} [/CODE]
0.1.9 — BETA 4.29