PDA

View Full Version : سوال: اکسپورت شماره موبایل توسط regex



#Elahe#
سه شنبه 30 آذر 1395, 05:47 صبح
سلام
من چند هزار خط این شکلی دارم


Line 126: <Mobile>933.......</Mobile>
Line 146: <Mobile>919.......</Mobile>
Line 166: <Mobile>903.......</Mobile>
Line 187: <Mobile>912.......</Mobile>

تو notepad++ توسط عبارت باقاعده (?s)<[^><]*?> تگهای mobile رو میتونم حذف کنم (در واقع با کاراکتر خالی ریپلیس میکنم)
ولی اون عبارتهای line xxxx ها میمونن

چجوری "فقط" این شماره موبایلاشو اکسپورت بگیرم از متن ؟

#Elahe#
سه شنبه 30 آذر 1395, 06:07 صبح
اینجا چرا به خط 55 ارور میده ؟


<?php

function after($this, $inthat)
{

if(!is_bool(strpos($inthat, $this)))
{
return substr($inthat, strpos($inthat,$this)+strlen($this));
}
else
{
return false;
}
}

function before($this, $inthat)
{
return substr($inthat, 0, strpos($inthat, $this));
}

function between($this, $that, $inthat)
{
return before($that, after($this, $inthat));
}

function occurrencecount($this, $that, $inthat, $count)
{

$newinthat = after("$that", after("$this", $inthat));
if($newinthat != false)
{
$count = $count + 1;
return occurrencecount($this, $that, $newinthat, $count);
}
else
{
return $count;
}
}

$xml = ' Line 6: <Mobile>9308019820</Mobile>
Line 26: <Mobile>9128019820</Mobile>
Line 46: <Mobile>9193158189</Mobile>
Line 66: <Mobile>9903158189</Mobile>
Line 86: <Mobile>921060044</Mobile>
Line 106: <Mobile>9422060044</Mobile>
Line 126: <Mobile>9538708767</Mobile>
Line 146: <Mobile>9008708767</Mobile>
Line 166: <Mobile>9168708767</Mobile>
Line 187: <Mobile>9133134699</Mobile>';

while (occurrencecount('<mobile>','</mobile>',$xml,'0')){
$ex = between ('<mobile>','</mobile>',$xml); $xml = str_replace ('<mobile>'.$ex.'</mobile>','',$xml); $ex .= '\n\r'.$ex;
}
echo $ex;

?>

plague
سه شنبه 30 آذر 1395, 18:35 عصر
وقتی داده ها ساختار تکرار شونده و مشخصی دارن نیاز نداری از ریجکس استفاده کنی خیلی کدش ساده و کمتر میش اگه با اسکپلود ساختار رو بشکونی و دیتا رو بکشی بیرون



$mobiles = array();
foreach(explode('</Mobile>' , $xml ) as $m )
{
$mm = explode('<Mobile>' , $m);
$mobiles[] = end($mm);
}