Jump to content

[SOLVED] match everything between = and \x22>


Recommended Posts

I have a list with html urls

<a href="http://www.website.tld/home.php?search=1&results=20&query=cool+website">cool website</a>, 

 

I can match <a href="http://www.website.tld/home.php?search=1&results=20&query= by using

<a href=\x22http://www.website.tld/home.php?search=1&results=20&query=

 

But I have a problem with the part between 'query=' and '">'. This can be everything: letters, digits, quotes, /,...

 

How do I tell php to match all possible characters and length?

 

Link to comment
https://forums.phpfreaks.com/topic/135554-solved-match-everything-between-and-x22/
Share on other sites

Do I undrestand you corrcetly when I propose this?

$str = '<a href="http://www.website.tld/home.php?search=1&results=20&query=cool+website">cool website</a>';
preg_match('#<a href="([^"]*)"#', $str, $match);
echo $match[1];

 

Output:

 

http://www.website.tld/home.php?search=1&results=20&query=cool+website

 

EDIT: Admittedly, your post is not very descriptive, in that you are not showing on a line by itself a sample of what exactly you are trying to match (based on your initial a tag). If I am off base on what I am proposing, then I am misunderstanding what it is exactly you are looking for. Give me a sample match portion from your initial example string (in otherwords, show me exactly what part you want to match from your string). Beacuse this thread is called match everything between = and \x22>, but yet in the example, you have: <a href=\x22http://www.website.tld/home.php?search=1&results=20&query=

 

well, the = and \x22 is right beside each other.. so its confusing.

 

If I did nail it, then it's all good.

My post was indeed not very clear as it's the first time I'm working with regex. I made a mistake by using str_replace and not preg_replace :-[

 

The code you gave me (I added a dot after ") helped me, but I still didn't get the output right.

#<a href="([^"]*)".#

 

This is wat I want to get as output;

 

1. I have a list with urls

<a href="http://www.website.tld/home.php?search=1&results=20&query=cool+website">cool website</a>, 
<a href="http://www.website.tld/home.php?search=1&results=20&query=best+forum">best forum</a>, 
<a href="http://www.website.tld/home.php?search=1&results=20&query=tryme">tryme</a>, 
...

 

2. I just want to show the part between '>' and '</a>, ' with a '<br>' at the end

cool website<br>
best forum<br>
tryme<br>
...

Ah, well that explains things much more clearly... try this:

 

$str = '<a href="http://www.website.tld/home.php?search=1&results=20&query=tryme">tryme</a>';
preg_match('#<a[^>]*>(.+?)</a>#', $str, $match);
echo $match[1];

 

Output:

tryme

your code works with the string. thank you :D Now I'm trying to run this for the complete list. I made a code (on the base of some other codes), but I only get a few words ???

<?php

$file = file_get_contents('urls.php');
$expl = explode("\n",$file);
$new = implode("\n",$expl);
preg_match('#<a[^>]*>(.+?)</a>#', $new, $match);
echo $match[1];

?>

Ah, you are getting these links in a file.

I suppose one way would be to use preg_match_all instead of just preg_match, and apply that to $file:

 

$file = file_get_contents('urls.php');
preg_match_all('#<a[^>]*>(.+?)</a>#s', $file, $matches);
echo '<pre>'.print_r($matches[1], true);

 

Would this work out?

 

 

lol not at all.. sorry about that...its because of how I presented the end results with this last line:

 

echo '<pre>'.print_r($matches[1], true);

 

You don't have to use that if you don't want to.. you could just use a foreach function to tap into $matches[1] and go that way instead:

 

foreach($matches[1] as $val){
   echo $val . '<br />';
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.