How to get the words between the characters "--#" and "#--"
I tried regex and explode but I cant get the desired output.
output must be : Section 2, Section 3, Section 4, Section 5
.................................................................................
--#Section 2#--
-##Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt##-
--#Section 3#--
-##Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt##-
--#Section 4#--
-##Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt##-
--#Section 5#--
-##Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt##-
Since the title of the question clearly states:
get the words between two specific characters in php....
You can use this regex:
preg_match_all('/--#(.*?)#--/', $text, $matches);
print_r($matches[1]);
Explanation:
--# # match '--#'
( # group and capture to \1:
.*? # any character except \n (0 or more times)
) # end of \1
#-- # match '#--'
Working Demo
Through sed,
$ sed 's/.*--#\(.*\)#--.*/\1/g' file
Section 2
Section 3
Section 4
Section 5
You could try this one:
--#(.*)#--
DEMO
If you use BASH
a simple answer without using sed or any other command is by doing the following
VAR="--#Section 2#-- -##Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt##-"
#delete from the tail
VAR=${VAR%%'#--'*} //you will get VAR="--#Section 2"
#delete from the head
VAR=${VAR##*'--#'} //NOW you'll have VAR="Section 2"
Related
I need to remove specific tags and text between them. For tags I can use str_replace but for text between I try using preg_replace but nothing works.
Code to fix:
// ...
[tabs_item title=”My title”] some different text here... [/tabs_item]
[tabs_item title=”My Other title”] some different text here... [/tabs_item]
// ...
my code:
$pattern = "/[tabs_item[^>]*title=\"my txt\"](.*?)[\\/tabs_item]/si";
$replacement = '';
$fix1 = preg_replace($pattern, $replacement, $content2);
$search = array('[/tabs_item]');
$replace = array('');
$fix2 = str_replace($search, $replace, $fix1);
return $fix2;
Thank you for posting full code. This needs to be handled a little differently since there is html in your content.
First, because your content contains the / char, we need to change the regex delimiter to something else so it doesn't interfere. I changed it to %. Next, since you're using a multi-line string, we need to set a modifier on the regex to include newline chars in the .(dot: all chars). This modifier is added by adding the 's' to the end of your pattern after the closing delimiter.
This code is tested and worked for me.
$content = '
<p>[tabs] [tabs_item title="Description"]<br />
Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet<br />
Lorem ipsum dolor sit amet<br />
Lorem ipsum dolor sit amet</p>
<p>[/tabs_item] [tabs_item title="Zwroty"]</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce nec mauris nunc. Vestibulum elit urna, aliquet et urna et, sagittis blandit ligula.</p>
[/tabs_item]
<p>[tabs_item title="Peyment and shipping"]</p>
<p>Lorem ipsum dolor sit amet</p>
<p><b>Lorem ipsum dolor sit amet</b>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet " url"</p>
<p>[/tabs_item]</p>
<p>[/tabs]</p>';
$pattern = "%\[tabs_item[^\[]+\[/tabs_item\]%s";
$replacement = '';
$out = preg_replace($pattern, $replacement, $content);
echo $out;
Results in:
<p>[tabs]
<p></p>
<p>[/tabs]</p>
To answer your followup question: If you want to remove only this tag:
[tabs_item title="Peyment and shipping"]...[/tabs_item]
you can change your pattern to:
$pattern = '%\[tabs_item title="Peyment and shipping"\][^\[]+\[/tabs_item\]%s';
Which results in:
<p>[tabs] [tabs_item title="Description"]<br />
Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet<br />
Lorem ipsum dolor sit amet<br />
Lorem ipsum dolor sit amet</p>
<p>[/tabs_item] [tabs_item title="Zwroty"]</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce nec mauris nunc. Vestibulum elit urna, aliquet et urna et, sagittis blandit ligula.</p>
[/tabs_item]
<p></p>
<p>[/tabs]</p>
If you no have regexp skill you can make this with explode function like this;
<?php
$var = '// something
[tabs_item title=”My title”] some different text here... [/tabs_item]
[tabs_item title=”My Other title”] some different text here... [/tabs_item]
// something';
$first = explode('[tabs_item title=”My title”]',$var);
$start = $first[0];
$last = explode('[/tabs_item]',$first[1]);
$end = $last[count($last)-1];
echo $start.$end;
?>
I have problem with regex tag html. Any one please help me!
Thanks this is some case of me... I have search and think but not do it.
Case 1
// My input to regex
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit <br/><img src="img.jpg/> sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua<p>
// Out Put after regex
Lorem ipsum dolor sit amet, consectetur adipisicing elit <br/><img src="img.jpg/> sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua
Case 2
// My input to regex
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>
// Out put after regex
Lorem ipsum dolor sit amet, consectetur adipisicing elit
Case 3
// My input to regex
<p><ul>...</ul><p>
//Out put after regex
NULL
I'm guessing something like this is what you're after (example in javascript).
function checkParagraph(str)
{
var result = str.match(/^<p>([^<].*[^>])<\/p>$/i);
if (result) return result[1];
else return null;
}
alert(checkParagraph("<p>Lorem ipsum <br/><img src=\"img.jpg\"/> magna aliqua</p>"));
alert(checkParagraph("<p>Lorem ipsum magna aliqua</p>"));
alert(checkParagraph("<p><img src=\"img.jpg\"/></p>"));
With the additional information about only allowing BR, IMG, A and IMG-inside-A tags, the regex is quite different:
function checkParagraph(str)
{
var result = str.match(/^<p>(([^<>]+|<br\/>|<img[^>]+>|<a[^>]+>[^<>]*<\/a>|<a[^>]+><img[^>]+><\/a>)*)<\/p>$/i);
if (result) return result[1];
else return null;
}
alert(checkParagraph("Lorem ipsum magna aliqua"));
alert(checkParagraph("<p>Lorem ipsum magna aliqua</p>"));
alert(checkParagraph("<p>Lorem ipsum <br/> magna aliqua</p>"));
alert(checkParagraph("<p>Lorem ipsum magna aliqua</p>"));
alert(checkParagraph("<p>Lorem ipsum <img src=\"img.jpg\"/> magna aliqua</p>"));
alert(checkParagraph("<p>Lorem ipsum <br/><img src=\"img.jpg\"/> magna aliqua</p>"));
alert(checkParagraph("<p><br/><img src=\"img.jpg\"/></p>"));
alert(checkParagraph("<p><span>magna</span> aliqua</p>"));
alert(checkParagraph("<p><span>magna</span> aliqua</p>"));
alert(checkParagraph("<p><br/><img src=\"img.jpg\"/><span>magna</span> aliqua</p>"));
Break-down of the regex:
/.../i -> case insensitive for upper and lower case tags
^<p>...<\/p>$ -> input is enclosed in P tag
(...) -> the capture group between the brackets will become result[1]
(...|...)* -> any number of the following options:
[^<>]+ -> option 1: any text without tags
<br\/> -> option 2: a BR tag
<img[^>]+> -> option 3: an IMG tag
<a[^>]+>[^<>]*<\/a> -> option 4: an A tag with text inside
<a[^>]+><img[^>]+><\/a> -> option 5: an A tag with an IMG tag inside
How would I use PHP's preg_replace() to return only the value inside the <h1> in the following string (it's HTML text loaded in a variable called $html):
<h1>I'm Header</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tincidunt porttitor magna, quis molestie augue sagittis quis.</p>
<p>Pellentesque tincidunt porttitor magna, quis molestie augue sagittis quis. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
I've tried this: preg_replace('#<h1>([.*])</h1>.*#', '$1', $html), but to no avail. Am I regex-ing this correctly? And is there a better PHP function that I should be using instead of preg_replace?
Here is how you do it using preg_replace:
$header = preg_replace('/<h1>(.*)<\/h1>.*/iU', '$1', $html);
You can also use preg_match:
$matches = array();
preg_match('/<h1>(.*)</h1>.*/iU', $html, $matches);
print_r($matches);
([.*]) means dot OR astersk
What you need is (.*?), which means any amount of any characters ungreedy
or
([^<]*) - which means any amount of any characters but not <
I have input text that can be long HTML text with tags and so on. Example of input can be something like:
<p>Lorem ipsum dolor sit amet, <strong>consectetur</strong> adipiscing elit.<p>
<p>%image1%</p>
<h2>Lorem ipsum</h2>
<p>Cum sociis natoque penatibus et magnis dis parturient montes.</p>
<p>%image2%</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p>
<p>%image3%</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p>
...
What would be the easiest way of finding all occurrences of text between %% characters and replacing that with <img src="image1.jpg">?
try using preg_replace ('/%(.+?)%/', '<img src="image1.jpg">', $string); i might be a little bit off on the regex pattern as to rather u need to escape %, and if ? is the greedy symbol.
preg_replace('|%(.+)%|', '<img src="$1">', $text );
working example: http://codepad.org/20Oz3Vok
Try this:
preg_replace('|%(\w+)%|', '<img src="$1">', $string);
It will only allow alphanumeric characters, as well as underscores (to prevent the problem #AlexanderVarwijk pointed out in the comments).
If I fopen/fread a text file, then send the content in an email, the newlines in the text file do not appear in the email when it arrives.
For example, the text...
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Sed leo erat,
rutrum posuere justo.
... arrives in the email inbox as:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed leo erat, rutrum posuere justo.
I'm using this PHP code:
$fh = fopen($email_file,'r');
$message = fread($fh,filesize($email_file));
fclose($fh);
mail("email#example.com",$subject,$message,$headers);
//Assume each variable is declared and defined.
What am I missing?
Edit: I needed to change the header Content-Type from HTML for the text version. Thanks to Yzmir and Footie. Their questions / comments led me to the correct answer.
Try replacing the new lines with <br/> and let me know if this solves it.
If you can't do it for your files, do it in your code using:
$message = nl2br($message);