Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

searching for text within string

Posted on 2011-02-16
15
210 Views
Last Modified: 2012-05-11
I have a PHP whois script and within one of the strings is "Domain name: DOMAIN.COM"
I would like to find "DOMAIN.COM" so that I can "href it"
0
Comment
Question by:prowebinteractiveinc
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34906701
Well, it looks like it follow the colon, so the correct approach would be to use explode(':', $string) and then trim() position 1 of the resulting array.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34906723
If the line is always in format
"Domain name: DOMAIN.COM",

<?php
$line = "Domain name: DOMAIN.COM";
$domain = substr($line,12);
echo"$domain";
?>

Open in new window


If not - use preg_match or post the output / script code.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34906732
http://us.php.net/manual/en/function.explode.php
http://us.php.net/manual/en/function.trim.php
<?php // RAY_temp_prowebinteractive.php
error_reporting(E_ALL);

// TEST DATA FROM THE POST AT EE
$str = "Domain name: DOMAIN.COM";

// THE DELIMITER
$dlm = ':';

// PARSE THE STRING
$arr = explode($dlm, $str);
$new = trim($arr[1]);

echo $new;

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:prowebinteractiveinc
ID: 34907051
basically  of an array of values such as

$result['rawdata'][$i]

as I loop through the array I would like to search for the DOMAIN variable so I can href it
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34907569
As long as the domain is always at the end of the line, I would use a regex to take care of any variation in the early part of the string.

while( $result = ...... etc ) {

     $domain = preg_replace('#^.+?([\.-0-9a-zA-Z]+)\s*$#', '$1', $result['rawdata'][$i] );

     $link = "<a href='http://$domain'>$domain</a>";

     ..... etc
}
0
 

Author Comment

by:prowebinteractiveinc
ID: 34907698
whats is the $1 variable all about ?
0
 

Author Comment

by:prowebinteractiveinc
ID: 34907740
nevermind, I got the answer to that, however, what you sent me is not bad, but I want it to match the exact domain searched for, I dont wnat it picking up domain name servers, or registration providers domain
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34907779
In the pattern there is a bracketed group '#^.+?([\.-0-9a-zA-Z]+)\s*$#'  and in the replacement pattern (the second parameter) $1 represents that bracketed group.

What this statement is saying is "examine $result['rawdata'][$i] and if matches this pattern '#^.+?([\.-0-9a-zA-Z]+)\s*$#' then replace it with this '$1'."  In other words, if the pattern matches, the original data is completely replaced with the bracketed group represented by $1. So everything is removed and only the bracketed group which contains the domain data is retained.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34907802
"...I want it to match the exact domain searched for, I dont wnat it picking up domain name servers, or registration providers domain"

If you know what domain you are searching for, then why are you trying to pick that domain out? You already know what the domain is?

Sorry - but I'm confused.......
0
 

Author Comment

by:prowebinteractiveinc
ID: 34907817
regular expression not working properly, it duplicates the first letter of the domain, and does not match for upper and or lowercase
0
 

Author Comment

by:prowebinteractiveinc
ID: 34907838
bportlock:  as I mentioned, I want to HREF the domain in the HTML so if the person doing the search as sees that the domain is taken, can click on the search domain directly from the page

Make sense ?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34908058
So you want to replace the domain IN that string?

<?php

$test = "Domain name: DOMAIN.COM";

echo preg_replace('#^(.+?)([\.-0-9a-zA-Z]+)\s*$#', '$1<a href="http://$2">$2</a>', $test);

Open in new window

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34908205
"...regular expression not working properly, it duplicates the first letter of the domain, and does not match for upper and or lowercase..."

Is the string you are comparing exactly similar in format to the one you gave me as an example? If the format of the string is different then the regex will not match it.
0
 

Author Comment

by:prowebinteractiveinc
ID: 34908304
I must be really bad at explaining what I want because Im not being understood

if the domain is msn.com  I want to find msn.com in the string, however depending on the registrar it could show in lower, or upper case, so I want to find the msn.com in the string and replace by <a href="msn.com">msn.com</a>
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 34908689
OK - how about this?

<?php

$test = "Domain name: DOMAIN.COM";
$lookFor = "domain.com";

echo preg_replace('#('.$lookFor.')#is', "<a href='http://$lookFor'>\$1</a>", $test);

// or this

echo "<br/>";

echo preg_replace('#('.$lookFor.')#is', "<a href='http://$lookFor'>$lookFor</a>", $test);

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question