I want to grab href address which has class link [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
[a href="http://www.vanchosun.com/m/market_index.php?view=market"]11[/a]
[a href="test.php?view=test&cate=buysell&testid=100" class="link"]22[/a]
How to grab href which has class link using php preg_match_all
Thanks.

You can use this:
$pattern = '~(?(DEFINE)(?<class>\bclass\s*=\s*"[^"]*?\blink\b[^"]*"))
<a\s+ [^>]*?
(?| \g<class> [^>]*? \bhref\s*=\s*"([^"]*)"
| \bhref\s*=\s*"([^"]*)" [^>]*? \g<class>)~xi';
preg_match_all($pattern, $code, $matches, PREG_SET_ORDER);
foreach($matches as $match) {
echo '<br/>' . $match[1];
}
However peiman F. has a good answer, since the DOM is a better choice for this kind of task.
A DOM way:
$doc = new DOMDocument();
#$doc->loadHTML($code);
$links = $doc->getElementsByTagName('a');
foreach ($links as $link) {
if (preg_match('~\blink\b~i', $link->getAttribute('class')))
echo '<br/>' . $link->getAttribute('href');
}

for jobs like this its better to use php DOM
see the easy how to use tutorial
use DOMElement::getAttribute method in all method list
http://www.php.net/manual/en/domelement.getattribute.php

Related

How to add in every link (a href) tag a rel attribute with the help of php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a variable $mydata (my code is in php) which includes text and some pictures. Every picture included is in this form :
<img src="mypicture.jpg" alt="lala" title="lala" border="0" />
This variable $mydata which has text and images after is inserted into my database (mysql).
What i want to do is, with some way to add to every and each one of the link images into the a href tag, before the img tag, one more attribute and this is rel="lightbox" so that lightbox effect with be applied in all of my images, such as :
<img src="mypicture.jpg" rel="lightbox" alt="lala" title="lala" border="0" />
How to make this in php, can you help me please ?
I guess may we have to use some kind of regular expression in order to implement that but my knowledge of regular expressions is not so advance.. can you help me please ?
$pat = <<<pattern
~(<a href="[^"]+")(><img src="[^"]+" alt="[^"]+" title="[^"]+" border="[^"]+" /></a>)~
pattern;
$html = preg_replace($pat, "\\1 rel=\"lightbox\"\\2", $html);
If all the pictures in $mydata do not have the "rel" attribute, then solution is simple
$mydata = str_replace('<img ', '<img rel="lightbox"', $mydata);
In other cases, only if ">" character does not appear in attribute values​​, this code works
function rel_adder( $matches ) {
if ( strpos($matches[0], 'rel=') === false ) {
return $matches[1].'rel="lightbox" '. $matches[2];
} else {
return $matches[0];
}
}
$mydata = preg_replace_callback('#(<img )([^>]+)>#i', "rel_adder", $mydata);

PHP find function without found element html [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to figure out how to grab the string inside H1 without actually H1 inside the string.
$html = "<div id="test"><h1>hello world</h1></div>"
foreach($html->find('div[id=test] h1') as $test1){
echo '<div>';
echo $test1;
echo '</div>';
}
$test1 returns "<h1>hello world</h1>"
I'd like for $test1 to be just "hello world" and without the h1 tags. Thank you in advance
You can use regex for this:
$str = '<div id="test"><h1>hello world</h1></div>';
$matches = array();
preg_match('/<h1>(.*)<\/h1>/', $str, $matches);
echo $matches[1];
Outputs: hello world
Docs:
http://www.php.net/preg_match
Demos:
https://eval.in/78867
http://regex101.com/r/lG3dO8

Break string into fields [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string input using PHP that look like this:
Log,,t1,12.12:t2,20.23:t3,30.00:t4,50.20:11.23
I want to save it into MySQL
field:reading
t1:12.12
t2:20.23
t3:30.00
t4:50.20
Temp:11.23
Can anyone give me an direction?
Begin you have to parse to rows and columns
$string = 't1,12.12:t2,20.23:t3,30.00:t4,50.20:11.23';
$rows = explode(':', $string);
$table = array();
foreach(rows as $row) {
$table[] = explode(',', $row);
}
You can use regex below:
if (preg_match_all('/[a-z]+\d+,\d+\.\d+|\d+\.\d+/i', $string, $matches) {
var_dump($matches[0]);
}
The regex can be a bit simpler if each field will called "t+number":
if (preg_match_all('/t\d+,\d+\.\d+|\d+\.\d+/i', $string, $matches) {
var_dump($matches[0]);
}

Extract the 'image' attribute from this chunk of text via regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I can find many examples of finding URL's withing HTML, however, I need to be more specific and extract the URL for the image in the following piece of text:
I will only need the first match.
I am using PHP.
Expected value result: http://cdn.somewebsite.com/path/123.jpg
var flashvars = {
'url_mode':'1',
'image':'http://cdn.somewebsite.com/path/123.jpg',
'bufferlength':'3',
'id': 'player',
'autostart': 'true'
...
};
Using preg_match with positive lookbehind assertion:
$data = <<< EOF
var flashvars = {
'url_mode':'1',
'image':'http://cdn.somewebsite.com/path/123.jpg',
'bufferlength':'3',
'id': 'player',
'autostart': 'true'
...
};
EOF;
preg_match("/(?<='image':')[^']+/", $data, $match);
echo($match[0]);
Hi this should work for you.
preg_match(/http.*?\.jpg/)

How do i extract a number from a url using PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an SMF website and i'm actually trying to get some header information which includes the title of a particular thread, the url but i've been finding it difficult to get the unique link affixed to the url using PHP.
Here's the url: http://example.com/index.php?topic=6449.msg6858
I'm actually looking for a way to extract the number 6449, I've tried to use the php GET function but it doesn't work.
$parts = explode('.', $_GET['topic']);
echo $parts[0];
// PHP 5.4+
echo explode('.', $_GET['topic'])[0];
See it in action
This would work, too
echo (int) $_GET['topic'];
See it in action
You want to use a combination of substr and strpos (to find the first occurence of a period)
$number = substr($_GET['topic'], 0, strpos($_GET['topic'], '.'));
// 6449
$arr = array();
if (preg_match("/([\\d]+)([.]{1}msg[\\d]+)/", $_GET["topic"], $arr) == 1) {
echo $arr[1];
} else {
trigger_error("not found", E_USER_ERROR);
}

Categories