php preg_match add href .html [duplicate] - php

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 5 years ago.
I would like to change all href's in a website.
xyz …
to:
<a href="sitename.html>xyz …</a>
Thanks for help!

You could do it using the following way ...
$hrefs = 'xyz …
xyz …';
$r = '/(?<=href=").*?(?=">)/';
$sitename = 'sitename.html';
$result = preg_replace($r, $sitename, $hrefs);
echo $result;
DEMO

$content = file_get_contents('page.html');
$new_content = str_replace('<a href="', '<a href="sitename.html', $content);
echo $new_content;

Related

Transforming <span style="font-weight:bold">some text<span> into <b>some text</b> in PHP [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
As title, If I have some html <p><span style="font-style:italic">abcde</span><span style="font-weight:bold">abcde</span></p>, I want to strip the style tags and transform them into html tags, so to make it become <p><i>abcde</i><b>abcde</b></p>. How can I do that in PHP?
I notice that when I open the html in CKEditor, this kind of transformation is done automatically. But I want to do it in backend PHP. Thanks.
$string = '<p><span style="font-style-italic;font-weight:bold">abcde</span><span style="font-weight:bold">abcde</span></p>';
$dom = new DOMDocument();
$dom->loadHTML($string);
$xp = new DOMXPath($dom);
$str = '';
$results = $xp->query('//span');
if($results->length>0){
foreach($results as $result){
$style = $result->getAttribute("style");
$style_arr = explode(";",$style);
$style_template = '%s';
if(count($style_arr)>0){
foreach($style_arr as $style_item){
if($style_item == 'font-style-italic'){
$style_template = '<i>'.$style_template.'</i>';
}
if($style_item == 'font-weight:bold'){
$style_template = '<b>'.$style_template.'</b>';
}
}
}
$str .= sprintf($style_template,$result->nodeValue);
}
}
$str = '<p>'.$str.'</p>';
You can also use html tags under php parameters or php opening and closing tags like this
<?php
echo"<h1>Here is Heading h1 </h1>";
?>
Or you can Put your html code in " " after echo
Like this
<?php
echo"Your Html Code Here";
?>
$output = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $input);
Match a < follow by one or more and not > until space came and the style="anything" reached. The /i will work with capital STYLE and $1 will leave the tag as it is, if the tag does not include style="". And for the single quote style='' use this:
(<[^>]+) style=("|').*?("|')

How to extract title from php file get contents? [duplicate]

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;

get href value by class name regex and php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
i need to get href value from class name like that.
some one can give me regex to find this class name and get from there the href ?
thanks alot
You can do this using DomDocument and XPath, see here
$str = '';
$doc = new DOMDocument();
$doc->loadHTML($str);
$xpath = new DOMXPath($d);
$links = $xpath->query('//a[#class="boldOrange"]');
foreach ($links as $link) {
$url = $link->getAttribute('href');
print $url . PHP_EOL;
}

Regular Expression not match href strings [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I have the following regex:
$regex = '<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>';
how can I improve this to NOT match the word "files" or "resize" in the href tag:
link or
yes parsing is the mutch better way to do this - maybe someone find this helpful:
$inhalt = new DOMDocument;
$inhalt->loadHTML($content->draw()[0][0]);
foreach ($inhalt->getElementsByTagName('a') as $node) {
if ($node->hasAttribute('href')) {
if (preg_match("/(files|resize)/", $node->getAttribute('href')) == 0) {
$node->setAttribute('href', 'mobile.php?uri=http://www.example.com' . str_replace("..", "", $node->getAttribute('href')));
$inhalt->saveHtml($node);
}
}
}
echo $inhalt->saveHtml();
You can use this regex to get all href string:
<a[^>]*href=[\"\'](.*?)[\"\'][^>]*>(.*?)</a>

PHP make links using preg_replace? [duplicate]

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;
}
?>

Categories