Clear in regular expression php - php

How I can replace string inside some text to getting this string without this "pattern"?
For example I trying replace %%some text%% to
<span class="spoiler">some text</span>
preg_replace("'%%[\w\s]+%%'siu",'<span class="spoiler">$0</span>',$description);

This will do what you are looking for:
$description = '%%some text%%';
$fixed_description = preg_replace("~%%([\w\s]+?)%%~siu",'<span class="spoiler">$1</span>',$description);
echo $fixed_description;
Output:
<span class="spoiler">some text</span>

Related

String Replace UFT-8 and ApphaNumeric In php?

Input string:
$string = "Dinesh G தினேஷ் ";
I Would Like to Convert this string into
<span class="english">Dinesh</span> <span class="english">G</span> தினேஷ்
Please help me resolve this issue.
Try this simplest code, It will check for characters A-Z , a-z then we capture those words and replace them with span tags around it.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string = "Dinesh G தினேஷ் ";
echo preg_replace("/([A-Za-z]+)/", '<span class="english">$1</span>', $string);
Output:
<span class="english">Dinesh</span> <span class="english">G</span> தினேஷ்

SIMPLE HTML DOM - how to ignore nested elements?

My html code is as follows
<span class="phone">
i want this text
<span class="ignore-this-one">01234567890</span>
<span class="ignore-this-two" >01234567890</span>
<a class="also-ignore-me">some text</a>
</span>
What I want to do is extract the 'i want this text' leaving all of the other elements behind. I've tried several iterations of the following, but none return the text I need:
$name = trim($page->find('span[class!=ignore^] a[class!=also^] span[class=phone]',0)->innertext);
Some guidance would be appreciated as the simple_html_dom section on filters is quite bare.
what about using php preg_match (http://php.net/manual/en/function.preg-match.php)
try the below:
<?php
$html = <<<EOF
<span class="phone">
i want this text
<span class="ignore-this-one">01234567890</span>
<span class="ignore-this-two" >01234567890</span>
<a class="also-ignore-me">some text</a>
</span>;
EOF;
$result = preg_match('#class="phone".*\n(.*)#', $html, $matches);
echo $matches[1];
?>
regex explained:
find text class="phone" then proceed until the end of the line, matching any character using *.. Then switch to a new line with \n and grab everything on that line by enclosing *. into brackets.
The returned result is stored in the array $matches. $matches[0] holds the value that is returned from the whole regex, while $matches[1] holds the value that is return by the closing brackets.

Add Brackets to Timestamp

I'm new to PHP so I don't know whether this is possible.
I need to add brackets to different timestamps so that this:
<span class="time">2:26</span>
<span class="time">2:51</span>
<span class="time">3:37</span>
<span class="time">1:19</span>
becomes this:
<span class="time">(2:26)</span>
<span class="time">(2:51)</span>
<span class="time">(3:37)</span>
<span class="time">(1:19)</span>
EDIT
The above HTML is generated using a simple DOM parser to grab info from a webpage.
If this is part of a larger HTML string or if the syntax might differ, it's a better idea to use a DOM parser.
However, if that isn't the case you can do this:
$string = str_replace('<span class="time">', '<span class="time">(', $string);
$string = str_replace('</span>', ')</span>', $string);
Or you can use regex:
$string = preg_replace('/<span class=\"time\">(\d+\:\d+)<\/span>/', '<span class="time">($1)</span>', $string);
Assuming your timestamp string is in $timestamp variable, you could use concatenation, ie.:
$output = '(' . $timestamp . ')';
Or as you mentioned, using a regular expression to validate the string before addind brackets:
$output = preg_replace("/(\d+):(\d+)/", "($0)", $timestamp);

replace line break with 2 spans in php

Consider the following HTML string:
<p>This is line 1 <br /> and this is line 2</p>
How can i replace the above with the following string using PHP / Regex
<p><span class="single-line">This is line 1</span><span class="single-line">and this is line 2</span></p>
This works but I would advise you not to rely on regular expressions for HTML parsing / transformation:
$string = '<p>This is line 1 <br /> and this is line 2</p>';
$pattern = '~([^<>]+)<br[[:blank:]]*/?>([^<>]+)~i';
$replacement = '<span class="single-line">$1</span><span class="single-line">$2</span>';
echo preg_replace($pattern, $replacement, $string);
I do not really suggest that you actually do it (because IMHO you're misusing markup and classes), however it's actually pretty simple:
you replace
<p>
with
<p><span class="single-line">
and
<br />
with
</span><span class="single-line">
and finally
</p>
with
</span></p>
A PHP function that can replace strings is strtrDocs.
Note that this works only for exactly the HTML fragment you've given in your question. If you need this more precise, you should consider using DOMDocument and DOMXPath as I already commented above.

How to grab the contents of HTML tags?

Hey so what I want to do is snag the content for the first paragraph. The string $blog_post contains a lot of paragraphs in the following format:
<p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p>
The problem I'm running into is that I am writing a regex to grab everything between the first <p> tag and the first closing </p> tag. However, it is grabbing the first <p> tag and the last closing </p> tag which results in me grabbing everything.
Here is my current code:
if (preg_match("/[\\s]*<p>[\\s]*(?<firstparagraph>[\\s\\S]+)[\\s]*<\\/p>[\\s\\S]*/",$blog_post,$blog_paragraph))
echo "<p>" . $blog_paragraph["firstparagraph"] . "</p>";
else
echo $blog_post;
Well, sysrqb will let you match anything in the first paragraph assuming there's no other html in the paragraph. You might want something more like this
<p>.*?</p>
Placing the ? after your * makes it non-greedy, meaning it will only match as little text as necessary before matching the </p>.
If you use preg_match, use the "U" flag to make it un-greedy.
preg_match("/<p>(.*)<\/p>/U", $blog_post, &$matches);
$matches[1] will then contain the first paragraph.
It would probably be easier and faster to use strpos() to find the position of the first
<p>
and first
</p>
then use substr() to extract the paragraph.
$paragraph_start = strpos($blog_post, '<p>');
$paragraph_end = strpos($blog_post, '</p>', $paragraph_start);
$paragraph = substr($blog_post, $paragraph_start + strlen('<p>'), $paragraph_end - $paragraph_start - strlen('<p>'));
Edit: Actually the regex in others' answers will be easier and faster... your big complex regex in the question confused me...
Using Regular Expressions for html parsing is never the right solution. You should be using XPATH for this particular case:
$string = <<<XML
<a>
<b>
<c>texto</c>
<c>cosas</c>
</b>
<d>
<c>código</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
/* Busca <a><b><c> */
$resultado = $xml->xpath('//p[1]');

Categories