PHP Archives - IAMCP-US https://www.iamcp-us.org Thu, 01 Jul 2021 12:20:40 +0000 en-US hourly 1 https://wordpress.org/?v=6.4.3 https://www.iamcp-us.org/wp-content/uploads/2021/07/cropped-code-blocks-icon-32x32.png PHP Archives - IAMCP-US https://www.iamcp-us.org 32 32 PHP Regular Expressions: preg_match, preg_split, preg_replace https://www.iamcp-us.org/php-regular-expressions-preg_match-preg_split-preg_replace/ https://www.iamcp-us.org/php-regular-expressions-preg_match-preg_split-preg_replace/#respond Thu, 01 Jul 2021 12:19:19 +0000 https://www.iamcp-us.org/?p=521 Regular expressions are a powerful pattern matching algorithm that can be performed in a single expression. Regular expressions use arithmetic operators such as (+, -, ^) to create complex expressions. Regular expressions help you perform tasks such as validating email …

The post PHP Regular Expressions: preg_match, preg_split, preg_replace appeared first on IAMCP-US.

]]>
Regular expressions are a powerful pattern matching algorithm that can be performed in a single expression. Regular expressions use arithmetic operators such as (+, -, ^) to create complex expressions. Regular expressions help you perform tasks such as validating email addresses, IP addresses, and more.

Why use regular expressions

  • Regular expressions make it easy to identify patterns in string data by calling a single function. This saves us coding time.
  • When validating user input such as email address, domain names, phone numbers, IP addresses.
  • Highlight keywords in search results.
  • When creating a custom HTML template. Regular expressions can be used to identify template tags and replace them with actual data.

PHP Regular Expressions

PHP has built-in functions that allow us to work with regular expressions. Let’s now take a look at the commonly used regular expression functions in PHP.

preg_match – This function is used to match against a pattern in a string. It returns true if a match is found and false if no match is found.
preg_split – This function is used to match against a pattern in a string and then splits the results into a numeric array.
< code> preg_replace – This function is used to match against a string pattern and then replace the match with the specified text.
The following is the syntax for a regular expression function such as preg_match , preg_split or preg_replace :

<?php
function_name('/pattern/',subject);
?>

“Function_name (…)” is either preg_match, preg_split, or preg_replace.
“/ … /” The forward slash marks the beginning and end of our regular expression.
“/ Pattern /” is the pattern we need.
“Subject” is the text string to match against.

Let’s now take a look at practical examples that implement the aforementioned regex functions in PHP.

PHP preg_match

In the first example, the preg_match function is used to simply pattern match the word it-blog in a given URL.

The code below shows the implementation for the above example.

<?php
$my_url = "www.iamcp-us.org";
if (preg_match("/iamcp-us.org/", $my_url))
{
    echo "String $my_url contains iamcp-us.org";
}
else
{
    echo "String $my_url not contain iamcp-us.org";
}
?>

PHP preg_split

Let’s now take a look at another example that uses the preg_split function.

We will take a string phrase and create an array from it using the pattern: element / space.

The text string to use in this example is “I love regular expressions.”

The code below illustrates the implementation of the above example.

<?php

$my_text="Regular expressions";

$my_array  = preg_split("/ /", $my_text);

print_r($my_array );

?>

PHP preg_replace

Let’s now take a look at the preg_replace function, which performs pattern matching and then replaces the pattern with something else.

The code below looks for the word it-blog in the string.

It replaces the word “iamcp-us” with the word “iamcp-us” surrounded by CSS code that highlights the background color.

<?php

$text = "Programming. iamcp-us.org";

$text = preg_replace("/iamcp-us.org/", '<span style="background:yellow">it-blog.ru</span>', $text);

echo $text;

?>

The post PHP Regular Expressions: preg_match, preg_split, preg_replace appeared first on IAMCP-US.

]]>
https://www.iamcp-us.org/php-regular-expressions-preg_match-preg_split-preg_replace/feed/ 0
Remove value from array in PHP https://www.iamcp-us.org/remove-value-from-array-in-php/ Mon, 01 Apr 2019 10:17:54 +0000 https://www.iamcp-us.org/?p=31 Deleting a value from an array by key in PHP is quite a simple task, but when it is necessary to delete an element of an array exactly by value, then there are slight difficulties. It seems to be a …

The post Remove value from array in PHP appeared first on IAMCP-US.

]]>
Deleting a value from an array by key in PHP is quite a simple task, but when it is necessary to delete an element of an array exactly by value, then there are slight difficulties. It seems to be a trivial task, but you have to use a little trick. In this article, we will look at how to delete an array element in PHP both by value and by key.

Remove value from array by key

Everything is very simple here. We need the PHP function unset (), which removes variables. With its help, you can also delete an array element by key. In the example below, we are removing the element with the key name from the $ array:

<?
$array = array('name' => 'Иван', 'lastname' => 'Иванов', 'site' => 'https://it-blog.ru');
unset($array['name']);
?>

As you can see, everything is quite simple, but how to remove an element from an array by its value, because the unset () function can find an element only by its key. You will learn more about this later.

Remove array element by value

To remove an element of an array by value, we will also use the unset () function, which removes elements by key, but first we will find an element by value. Another function array_search () will help us with this, which searches for an element by value and returns its key. Further, using the found key, we will remove the desired element from the array.

<?
$array = array('name' => 'Иван', 'lastname' => 'Иванов', 'site' => 'https://it-blog.ru');

if(($key = array_search('Иван', $array)) !== false){
     unset($array[$key]);
}
?>

As you can see, deleting an array element by value is quite simple, you just need to think a little.

The post Remove value from array in PHP appeared first on IAMCP-US.

]]>