Replace paragraph tag using regex - php

I need to take the output of $one and make an array, however, the regex is not right because all I get back is an array with one key and one value; basically, the entire string. If there are 10 paragraph tags in the string then I should get 10 values in the array.
What is wrong with my regex?
Ideally, what I would like to have in the output array are two arrays, one with the paragraph tags and another with just the text between them.
$one = preg_replace( '/<p>/i', '<p class="test">', $str ); // This gives me what I need
print_r(explode( '/<p class="test">/iU', $one )); // This does not

The problem is simple. Explode does not use regex. This should work for you.
print_r(explode('<p class="test">', $one ));
EDIT: This ought to do what you want.
$pattern = '/(?P<open><p class="test">)'
. '(?P<content>.*)'
. '(?P<close><\/p>)/i';
preg_match_all($pattern, $one, $matches);
print_r($matches);
EDIT: Simplified version without the match tags:
$pattern = '/(<p class="test">)(.*)(<\/p>)/i';
preg_match_all($pattern, $one, $matches);
print_r($matches);

Related

PHP preg_match an html arttribue with number seprated by a comma

I'm trying to get the values of an attribute and fill them in an array:
<p numbers="11.2,1.1 2,3,3.1 33"></p>
preg_match('/numbers="([0-9\\.]]+)"/',$elements,$match);
I need something like this:
$match[0] -> 11.2
$match[1] -> 1.1
$match[2] -> 2
Or anything that would get me to store the numbers in an array, but I can't figure out the regex for it.
Personally I would simply match all numbers and then split them
$matches = preg_match('/numbers="(.*)"/',$elements,$match);
$numbers = explode(',', $matches[1]);
I think I got one solution:
$string = '<tag> etc</tag>
<p numbers="11.2,1.1 2,3,3.1 33"></p>
<div> etc</div>';
// Catch only the numerals from the attribute numbers
preg_match('/<p.*numbers="(.*)"/', $string, $match);
// Catch each number separated by comma or space.
preg_match_all('/(\d+(\.?|\s?)(\d+)?)/', $match[0], $matches);
echo '<pre>';
print_r($matches[0]);
The regex is somehow pretty straightforward, but if you need some clarification, write a comment.
And you can see it tested here: https://3v4l.org/iVp1v

Replace multiple items in a string

i've scraped a html string from a website. In this string it contains multiple strings like color:#0269D2. How can i make str_replace code which replace this string with another color ?
For instance something like this just looping through all color:#0269D in the fulltext string variable?
str_replace("color:#0269D","color:#000000",$fulltext);
you pass array to str_replace function , no need to use loop
$a= array("color:#0269D","color:#000000");
$str= str_replace($a,"", $string);
You have the right syntax. I would add a check:
$newText = str_replace("color:#0269D", "color:#000000", $fulltext, $count);
if($count){
echo "Replaced $count occurrences of 'color'.";
}
This code might be too greedy for what you're looking to do. Careful. Also if the string differs at all, for example color: #0269D, this replacement will not happen.
’str_replace’ already replaces all occurrences of the search string with the replacement string.
If you want to replace all colors but aren't sure which hexcodes you'll find you could use preg_replace to match multiple occurrences of a pattern with a regular expression and replace it.
In your case:
$str = "String with loads of color:#000000";
$pattern = '/color ?: ?#[0-9a-f]{3,6}/i';
$replacement = "color:#FFFFFF";
$result = preg_replace($pattern, $replacement, $str);

How to strip and get the code between two lines in PHP?

I'm trying to assign it to a variable in PHP, but all the Regex and preg_replace I've tried doesn't help me. Here is a sample text.
Claim Code:
7241B-2HWRXR9-2P2BA
$1.00
I want to pull out exactly what is in the middle, which is 7241B-2HWRXR9-2P2BA.
You can use the following to match:
Claim Code:\s*([\w-]+)\s*\$(\d+(?:\.\d+)?)
And you can pull out whatt you want by $1
See DEMO
If the code has always the same format (5-7-5 chars), you can use:
$str = 'Claim Code:
7241B-2HWRXR9-2P2BA
$1.00';
preg_match('~[\w]{5}\-[\w]{7}\-[\w]{5}~', $str, $matches);
echo $matches[0]; // returns 7241B-2HWRXR9-2P2BA
UPDATE
For optional code length this regex is possible:
preg_match('~.*:\s+([\w\-]{10,25})\s+.*~', $str, $matches);
echo $matches[1]; // ^^ here set the min and max code length, or remove it
// without setting min/max code length:
preg_match('~.*:\s+([\w\-]+)\s+.*~', $str, $matches);
Less elegant than a regexp / preg_replace, but this should work, too: Put the string into an array, then only use line 2 (array element number 1).
<?php
$string = 'Claim Code: ...............';
$lines = explode("\n", $string); //Transform the string into an array, separated by new lines (\n) (each index in the array is a single line from the string
echo $lines[1]; //this is 2nd line of the string, i.e. the claim code

Find words from the array in the text received through file_get_contents

I have a receipt of a remote page:
$page = file_get_contents ('http://sayt.ru/');
There is a array of words:
$word = array ("word", "second");
How to count the number of words in the array matches the text on the page?
Started to dig in the direction
$matches = array ();
$count_words = preg_match_all ('/'. $word. '/ i',$page, $matches);
But certainly not in the direction I dig because count is always zero. And through preg_match_all sought after one word, not the entire array. : (
you have to either check or each word in array or use regexp like this:
$serachWords = array_map(function($w){ return preg_quote($w,'/'); }, $word);
$search = implode('|', $searchWords);
$count_words = preg_match_all('/\b(?:'.$serach.')\b/i', $page, $matches);
Added few modification to have better results: escape all words, so they wouldn't break expression and add word boundaries (\b) no match word as a word, not part of swords.

Regexp in php: how do I filter dynamic strings like abc/123/...?

I am trying to filter out all characters before the first / sign. I have strings like
ABC/123/...
and I am trying to filter out ABC, 123 and ... into separate strings. I have alsmost succeeded with the parsing of the first letters before the / sign except that the / sign is part of the match, which I don´t want to.
<?php
$string = "ABC/123/...";
$pattern = '/.*?\//';
preg_match($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
The letters before the first/ can differ both in length and characters, so a string could also look like EEEE/1111/aaaa.
If you are trying to split the string using / as the delimiter, you can use explode.
$array = explode("/", $string);
And if you are looking only for the first element, you can use array_shift.
$array = array_shift(explode("/", $string));

Categories