Belirli bir birine kadar bütün karakterleri preg_replace

4 Cevap php
&168491968426|mobile|3|100|1&185601651932|mobile|3|120|1&114192088691|mobile|3|555|5&

Mobil | | 3 | 120 | 1 & ((amp ile başlayan ve amfi ile biten) dikey çizgi, sadece ilk numarayı bilerek 185.601.651.932 ve ben, diyelim ki, bu bölümü &185601651932 silmek zorunda )

böylece sonuç i olurdu

&168491968426|mobile|3|100|1&114192088691|mobile|3|555|5&

Ben bu PHP preg_replace fonksiyonu ile nasıl yapabildin. Hat sayısı (|) değerleri her zaman aynı olurdu, ama yine de, id & arasındaki satır sayısına bağlı olarak değil, esnek bir model olması gibi ayrılmış imzalarlar.

Teşekkürler.

P.S. Ayrıca, ben php düzenli ifadeler ile iyi sadece yazılı kaynak bir bağlantı için minnettar olacaktır. Orada bol bol google vardır :) ama belki bir really great link var olur

4 Cevap

preg_replace("/&185601651932\\|[^&]+&/", ...)

Genelleştirilmiş,

$i = 185601651932;
preg_replace("/&$i\\|[^&]+&/", ...);

Gerçek esneklik istiyorsanız, preg_replace_callback kullanın. http://php.net/manual/en/function.preg-replace-callback.php

It seems to me you ought to be using another data structure than a string to manipulate this data. I'd want this data in a structure like

Array(
  [id] => Array(
     [field_1] => value_1
     [field_2] => value_2
  )
)

Sizin büyük dize böyle bir şey yaparak böyle bir yapıya masaj olabilir:

$data_str = '168491968426|mobile|3|100|1&185601651932|mobile|3|120|1&114192088691|mobile|3|555|5&';
$remove_num = '185601651932';

/* Enter a descriptive name for each of the numbers here 
- these will be field names in the data structure */
$field_names = array( 
    'number',
    'phone_type',
    'some_num1',
    'some_num2',
    'some_num3'
);

/* split the string into its parts, and place them into the $data array */
$data = array();
$tmp = explode('&', trim($data_str, '&'));
foreach($tmp as $record) {
    $fields = explode('|', trim($record, '|'));
    $data[$fields[0]] = array_combine($field_names, $fields);
}

echo "<h2>Data structure:</h2><pre>"; print_r($data); echo "</pre>\n";
/* Now to remove our number */
unset($data[$remove_num]);
echo "<h2>Data after removal:</h2><pre>"; print_r($data); echo "</pre>\n";