CodeIgniter Forums
How to change eregi() to preg_match() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: How to change eregi() to preg_match() (/showthread.php?tid=35337)



How to change eregi() to preg_match() - El Forum - 10-26-2010

[eluser]ibon[/eluser]
Please anyone help me,.


<?php
$headers = array_keys($csv[0]);
foreach ($headers as $v){
$hdr = trim(str_replace('"','',$v));
if ($hdr != '' && !eregi("thumbnail",$hdr) && !eregi("image",$hdr)){
echo "<th>".$hdr."</th>\n";
}
}
?&gt;

&lt;?php
foreach ($csv as $key => $line){
echo "<tr align='top'>\n";
foreach ($line as $f => $d) {
$FIELD = trim(str_replace('"','',$f));
$FDATA = trim(str_replace('"','',$d));
if ($FIELD != '' && !eregi("thumbnail", $FDATA) && !eregi("image", $FDATA)){
echo "<td>";
echo $FDATA. "\n";
echo form_hidden("line_$key"."[".$FIELD."]", $FDATA);
}
}
echo "</tr>\n";
}
?&gt;


How to change eregi() to preg_match() - El Forum - 10-26-2010

[eluser]WanWizard[/eluser]
Code:
preg_match('/thumbnail/i', $hdr)



How to change eregi() to preg_match() - El Forum - 10-26-2010

[eluser]InsiteFX[/eluser]
This may help others looking for this information.

Code:
Fix `ereg is deprecated` errors in PHP 5.3

If you upgraded to PHP 5.3, chances are high you’re going to run into a few warnings or deprecated function messages.
An example is the ereg family of functions, which are gone for good, as they were slower and felt less familiar than the alternative Perl-compatible preg family.

To migrate ereg():

ereg('\.([^\.]*$)', $this->file_src_name, $extension);

becomes

preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);


Notice that I wrapped the pattern (\.([^\.]*$)) around / /, which are RegExp delimiters. If you find yourself escaping / too much (for an URL for example), you might want to use the # delimiter instead.


To migrate ereg_replace():

$this->file_dst_name_body = ereg_replace('[^A-Za-z0-9_]', '', $this->file_dst_name_body);

becomes

$this->file_dst_name_body = preg_replace('/[^A-Za-z0-9_]/', '', $this->file_dst_name_body);


Again, I just added delimiters to the pattern.
If you are using eregi functions (which are the case-insensitive version of ereg), you’ll notice there’re no equivalent pregi functions. This is because this functionality is handled by RegExp modifiers.

Basically, to make the pattern match characters in a case-insensitive way, append i after the delimiter:

eregi('\.([^\.]*$)', $this->file_src_name, $extension);

becomes

preg_match('/\.([^\.]*$)/i', $this->file_src_name, $extension);

InsiteFX


How to change eregi() to preg_match() - El Forum - 10-26-2010

[eluser]ibon[/eluser]
to WanWizard : Thanks a lot, it work ! :-)
and
to InsiteFX : Thanks for references Smile

I still got a problem with this code and how to fixed ? especially split()


function parseLines($p_CSVLines) {
$content = FALSE;
foreach( $p_CSVLines as $line_num => $line ) {
if( $line != '' ) { // skip empty lines
$elements = split($this->separator, $line);

if( !is_array($content) ) { // the first line contains fields names
$this->_fields = $elements;
$content = array();
} else {
$item = array();
foreach( $this->_fields as $id => $field ) {
$item[$field] = $elements[$id];
}
$content[] = $item;
}
}
}
return $content;
}


How to change eregi() to preg_match() - El Forum - 10-26-2010

[eluser]ibon[/eluser]
It Work when I change with this :

preg_split("/$this->separator/", $line);