This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
The following is automatically output via editor:
1001-web-file
I would like to add an ID:
<a id="replace" href="https://someurl.com/1001-web-file.pdf">1001-web-file</a>
Then use str_replace and a regular expression to find the anchors in question and replace with:
<img src="https://someurl.com/1001-web-file-pdf-290x300.jpg"/>
What I've managed to do is:
$replace = array(
'<a id="pdfthumb" href="' => '<img src="',
'.pdf">pdfthumb</a>' => '-pdf-290x300.jpg"/></br>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
This works to tear down the anchor tag and rebuild as an img. But I can't do much more. I played around with some regex to create a wildcard and realized I need to create a variable for the href to use when I rebuild the HTML, but I'm stuck.
Any insight is much appreciated :)
In your case, I think it should be easier if you do it on client side using javascript.
My background is not PHP but you can use the same pattern to test with your PHP code:
Input:
1001-web-file
Output:
<img src="https://someurl.com/1001-web-file-pdf-290x300.jpg"/>
var input = '1001-web-file';
var pattern = /href=\"(.+)\.pdf\"/;
var match = input.match(pattern)[1];
input = input.replace(/(<a.*>).*(<\/a>)/, '$1<img src="' + match + '-pdf-290x300.jpg">$2');
console.log(input)
Here is translated php code of Tan javascript answer
$input = '1001-web-file';
preg_match('/href="(.+)\.pdf"/', $input, $m);
$match = $m[1];
$input = preg_replace('/(<a.*>).*(<\/a>)/', '$1<img src="'. $match .'-pdf-290x300.jpg">$2', $input);
echo $input;
Related
This question already has answers here:
Fastest way to retrieve a <title> in PHP
(7 answers)
Closed 3 years ago.
<?php
$content=file_get_contents('example.com');
// it would return html <head>.....
<title>Example.com</title>
I want to extract example.com from title
$title=pick('<title>','</title>',$content);
Echo $title;
And it would show Example.com
You can use substr to substring the HTML content and stripos to find the title tags.
I add 7 to the position to remove the tag.
$html = file_get_contents('example.com');
$pos = stripos($html, "<title>")+7;
echo substr($html, $pos, stripos($html, "</title>")-$pos);
Example:
https://3v4l.org/qvC40
This assumes there is only one title tag on the page, if there is more then it will get the first title tag.
You can use file_get_content() instead of $string.
$string = "<title>MY TITLE</title>";
$pattern = "/<title>(.*?)<\/title>/";
preg_match($pattern, $string, $matches);
echo "RESULT : ".$matches[1];
Try using PHP's simple xml parser to read the title node.
$xml = simplexml_load_string(file_get_contents('example.com'));
echo $xml->head->title;
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 6 years ago.
I have the following string in PHP:
$string = "<img src=\"url\" >HELLO WORLD<ol>I must replace the text after img and before ol.</ol>";
print htmlentities($string);
I want to find the substring HELLO WORLD (or whatever substring is, that is just an example of a text that will be completely dynamical), using the delimiters : "<img ... >" and "<ol>" and add <h3> delimiters. So the string above would result in:
<img src="url" ><h3>HELLO WORLD</h3><ol>I must replace the text after img and before ol.</ol>
I have tried the following code, of course with no success:
$string = preg_replace("/\<img (.*?)\> (.*?)\<ol\>/", "<img (.*?)><h3> (.*?)</h3></ol>", $string);
I know how to make very easy substituions, but the above condition is very far from my understanding.
I have found the answer by trial-and-error:
$string = "<img src=\"url\" >HELLO WORLD<ol>I must find the text after img and before ol.</ol>";
$string2 = preg_replace("/<img (.*?)>(.*?)<ol>/", "<img $1><h3>$2</h3><ol>", $string);
print htmlentities($string) . " <br />" . htmlentities($string2);
Explanation: I add the delimiters, and use $1 and $2 for matching the results between the delimiters in the right order.
This question already has an answer here:
XPath - select text after certain node
(1 answer)
Closed 9 years ago.
I have following string
<strong>Test: </strong> BD-F5300
I am interested in getting number BD-F5300. Number could be of any thing text,number.
Any help, how can I get it? Thanks.
You could make use of preg_replace
<?php
$str='<strong>Test: </strong> BD-F5300';
echo $str = preg_replace("~<(/)?strong>(.*?)<(/)?strong>~","", $str);
OUTPUT :
BD-F5300
do like this in JavaScript:
var src = "<strong>Test: </strong> BD-F5300";
var reg = /.*<\/.*>\s*([a-zA-Z0-9-]+)/g;
var group = reg.exec(src);
console.log(group[1]+'\r\n'); //group[1] is what you want !
If all you need is to get some content after </strong> then you can just use:
preg_match('#</strong> (.+)#', $string, $matches);
The desired match will be in $matches[1]. However, this requires that the <strong> tag and the text content you want to find are both on the same line. If there are multiples of these you want to match, you may want to use preg_match_all
If there is always a space before the beginning of the final text you want and if there are never any spaces in the actual number text you want, you can avoid regex by doing this:
$str = '<strong>Test: </strong> BD-F5300';
$solution = substr($str, strrpos($str, ' ') + 1);
var_dump($solution);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
replace any url's within a string of text, to clickable links with php
Just a quick question, When I post links like http://www.buddyweb.me it will just appear like that but, but t's not automaticly linked. So how can I replace the http://www.buddyweb.me with Google
Any suggestions are apreciated, thanks :)
Jut like here
function clickable($url){
$url = str_replace("\\r","\r",$url);
$url = str_replace("\\n","\n<BR>",$url);
$url = str_replace("\\n\\r","\n\r",$url);
$in=array(
'`((?:https?|ftp)://\S+[[:alnum:]]/?)`si',
'`((?<!//)(www\.\S+[[:alnum:]]/?))`si'
);
$out=array(
'<a href="$1" rel=nofollow>$1</a> ',
'<a href="http://$1" rel=\'nofollow\'>$1</a>'
);
return preg_replace($in,$out,$url);
}
$replaced = preg_replace('/(http[s]?:\/\/[^\s]*)/i', '$1', $url);
No need for preg-replace, just concatenate varibles around your link.
<?
$yourlink = "http://www.buddyweb.me";
$yourDescriptor = "Google";
$linkedlink = "$yourDescriptor";
echo $linkedlink;
?>
echo preg_replace("#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?#\[\]+]*)(/[\w\#$%&~/.\-;:=,?#\[\]+]*)?)#is", "\\1\\2", $string);
I would consider this a complicated Regular Expression. However, if you're interested in learning more, I really liked getting started with this video http://www.youtube.com/watch?v=DRR9fOXkfRE
Call something that will return it how you like.
<?php
$link = "http://stackoverflow.com";
$name = "Stack Overflow";
echo href($link, $name);
function href($link, $name){
$link = "$name";
return $link;
}
?>
Alright so I am using a little bbcode function for a forum I have, working well, so if, in example, I put
[b]Text[/b]
it will print Text in bold.
My issue is, if I have that code:
[b]
Text[/b]
Well it will not work, and just print that as it's right now.
Here is an example of the function I am using:
function BBCode ($string) {
$search = array(
'#\[b\](.*?)\[/b\]#',
);
$replace = array(
'<b>\\1</b>',
);
return preg_replace($search , $replace, $string);
}
Then when echo'ing it:
.nl2br(stripslashes(BBCode($arr_thread_row[main_content]))).
So my question would be, what is necessary so the BBcode works with everything inside it, but no necessarily on the same line.
In example:
[b]
Text
[/b]
Would simply be
Text
Thank you for any help!
Alex
You need the multiline modifier, which makes your pattern something like #\[b\](.*?)\[/b\]#ms
(note the trailing m)
There is actually a pecl extension that parses BBcode, which would be faster and more secure than writing it from scratch yourself.
I use this... It should work.
$bb1 = array(
"/\[url\](.*?)\[\/url\]/is",
"/\[img\](.*?)\[\/img\]/is",
"/\[img\=(.*?)\](.*?)\[\/img\]/is",
"/\[url\=(.*?)\](.*?)\[\/url\]/is",
"/\[red\](.*?)\[\/red\]/is",
"/\[b\](.*?)\[\/b\]/is",
"/\[h(.*?)\](.*?)\[\/h(.*?)\]/is",
"/\[php\](.*?)\[\/php\]/is"
);
$bb2 = array(
'\\1',
'<img alt="" src="\\1"/>',
'<img alt="" class="\\1" src="\\2"/>',
'<a rel="nofollow" target="_blank" href="\\1">\\2</a>',
'<span style="color:#ff0000;">\\1</span>',
'<span style="font-weight:bold;">\\1</span>',
'<h\\1>\\2</h\\3>',
'<pre><code class="php">\\1</code></pre>'
);
$html = preg_replace($bb1, $bb2, $html);