What to replace eregi() with? [duplicate] - php

This question already has answers here:
ereg/eregi replacement for PHP 5.3 [duplicate]
(4 answers)
Closed 5 years ago.
What is eregi() replaced with in this instance?
// get value of text inbetween tags
function getContentByTag($tag1, $tag2, $string)
{
if (eregi("$tag1(.*)$tag2", $string, $out)) {
$outdata = $out[1];
}
return $outdata;
}
This post is not a duplicate post as the 3rd example within the referenced post the answer is for that specific usage. I'm guessing my usage is different as the referenced post answers are not working for me.

Since PHP 7 you must replace it with
preg_match("/$tag1(.*)$tag2/i", $string, $out)

Related

How to put specific string in into array? [duplicate]

This question already has answers here:
PHP: Best way to extract text within parenthesis?
(8 answers)
Closed 3 years ago.
I have a string like this:
[Rise] and rise [again] until lambs become [lions].
I want to get all the string inside the [] (Rise, again, lions) and put it in to an array. How can I do this?
Edit: Thank you guys, I already found the solution here: PHP: Best way to extract text within parenthesis?
You can do it with regular expressions:
$s = '[Rise] and rise [again] until lambs become [lions].';
preg_match_all('/\[([^\]]+)\]/', $s, $m);
print_r($m[1]);

How to write a Function to scan & remove similar words from a large variable using PHP? [duplicate]

This question already has answers here:
How to remove duplicate values from an array in PHP
(27 answers)
Closed 6 years ago.
I was trying to generate keywords using dynamic data & hence thought this function could be useful for others hence i have answered the question myself.
Here is the function named scan_dups:
function scan_dups($value){
$end = implode(', ',array_unique(explode(', ', $value)));
echo $end;
}
Now when using, just put all your values inside a variable and put that variable inside this function.
Eg:
$a = "a,b,c,d,e,g,f,g,g,s,d,gt,te,h,a";
echo scan_dups($a);
And the output would be:
a,b,c,d,e,g,f,s,gt,te,h

Eregi_replace to preg_replace conversion needed [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
i need to convert this in order to use preg_replace because eregi_replace is deprecated
$n=eregi_replace(")+.+","",$value);
$id=eregi_replace(".+)","<a hсref='http://www.company.com/product.php?id=".$n."'>",$value);
$newstr.="".$id."</a><br>";
//..... must be converted
its very unclear to me how that would look in preg_replace
$gexpieces = explode(")", $value);
$n=$gexpieces[0];//n is product id
unset($gexpieces[0]);
$prodname = implode($gexpieces);
$newstr.="<a href='http://www.company.com/product.php?id=".$n."'>".$prodname."</a><br/><br/>";
nevermind, done it myself.

eregi() deprecated - I am not sure how to rewrite this code [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
How to validate an Email in PHP?
(7 answers)
How to validate an email address in PHP
(15 answers)
Closed 9 years ago.
Deprecated code:
function validate_email($email)
{
return eregi("^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email);
}
I am a JavaScript beginner. The above code gives an error. I am not too sure how to rewrite using preg_match.
Try this instead
function validate_email($email)
{
return (1 === preg_match("/^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email));
}
preg_match() should be used rather than eregi. You must include the slashes at the begining and end of the pattern. And finally, the "/i" at the end of the pattern makes it case-insensitive.

An Ampersand (&) before a function means? [duplicate]

This question already has answers here:
What does it mean to start a PHP function with an ampersand?
(3 answers)
Closed 7 years ago.
I know & is used to create references.
But I wonder what having a & before a function name means:
function &DB($params = '', $active_record_override = FALSE) { // code }
It returns the result by reference. See the manual entry for it here.

Categories