Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7701981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:07:16+00:00 2026-05-31T23:07:16+00:00

I am trying to extract a string from another string using php. At the

  • 0

I am trying to extract a string from another string using php.

At the moment im using:

<?php
  $testVal = $node->field_link[0]['view'];
  $testVal = preg_replace("#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie", "'<a href=\"$1\" target=\"_blank\">$3</a>$4'", $testVal);
  print "testVal = ";
  print $testVal;
?>

This seems to be printing my entire string at the moment.

Now what i want to do is: extract a web address if there is one and save it as a variable called testVal.

I am a novice so please explain what i am doing wrong. Also i have looked at other questions and have used the regex from one.

For @bos

Input:

<iframe width="560" height="315" src="http://www.youtube.com/embed/CLXt3yh2g0s" frameborder="0" allowfullscreen></iframe>

Desired Output

http://www.youtube.com/embed/CLXt3yh2g0s
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:07:18+00:00Added an answer on May 31, 2026 at 11:07 pm

    Well, you say you want to populate $testVal with the extracted web address, but you’re using preg_replace instead of preg_match. You use preg_replace when you wish to replace occurrences, and you use preg_match (or preg_match_all) when you want to find occurrences.

    If you want to replace URLs with links (<a> tags) like in your example, use something like this:

    <?php
    $testVal = preg_replace(
         '/((?:https?:\/\/|ftp:\/\/|irc:\/\/)[^\s<>()"]+?(?:\([^\s<>()"]*?\)[^\s<>()"]*?)*)((?:\s|<|>|"|\.||\]|!|\?|,|&#44;|&quot;)*(?:[\s<>()"]|$))/',
         '<a target="_blank" rel="nofollow" href="$1">$1</a>$2',
         $testVal
    );
    

    If you want to instead simply locate a URL from a string, try (using your regex now instead of mine above):

    <?php
    $testVal = $node->field_link[0]['view'];
    if(!preg_match("#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie", $testVal, $matches)) {
         echo "Not found!";
    else {
         echo "URL: " . $matches[1];
    }
    

    When you use preg_match, the (optional) third parameter is filled with the results of the search. $matches[0] would contain the string that matched the entire pattern, $matches[1] would contain the first capture group, $matches[2] the second, and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to extract a string from another using regex. I'm using the POSIX
I am trying to extract datetime info from 2012/04/03 10:06:21:611747 using format String dateformat
I'm trying to extract/match data from a string using regular expression but I don't
I am trying to extract the below pattern from a string using Ruby and
I am trying to extract substring from string gettting from mysql database using substr
I'm trying to extract a string from a text file using 2 delimiters. One
I'm trying to extract some string from a file using python re, then MD5ing
I am trying to extract all occurrences of tagged words from a string using
I am trying to extract data from a string using Regex in VB.net. This
I am new to Perl and I am trying to extract email from string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.