• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

Delete Before and after a string in PHP

I cannot find any info on deleting before and after a string.

example :

$string = 'Experts are ready to help solve your<a class ='problem' href='bla.php'>go there</a>. The quality of your question will directly ';

lets say i want the name of the href, how would i go about this?

Finished string


2 Solutions
Dave BaldwinFixer of ProblemsCommented:
According to HTML 4.01, <a hrefs do not have names.  Named anchors are for locating place within the current page.

In PHP, you can locate items in a string with strpos() or one of the other string position funtions found on this page: http://us.php.net/manual/en/function.strpos.php .  There are notes there about the "=== operator" for checking the results.
ScottNL1Author Commented:
I am using the href as a example.

can you find me with a php script everything after
and before
and put that into a string.

or everything after '> and before </
the string should be "go there"

I just want find out how to do this.

I thank you in advance.

Dave BaldwinFixer of ProblemsCommented:
There are a lot of examples on the php.net pages.  Usually you use strpos() to find the start of what you want and then substr() to extract it.  You actual method will depend on the data you're working with.

Your string example above will fail because you have 6 single quotes and no way for php to know what you mean.  Normally, you would either use double quotes on the ends or escape (\') the single quotes in the middle so php know where the correct start and end of the string is.
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Try my below code...its resolve your problem...
$string = "Experts are ready to help solve your<a class ='problem' href='bla.php'>go there</a>. The quality of your question will directly";

print '<pre>';

echo $split_str[3];


Open in new window

This code will show you all links and related link text in your given string
$str = 'Experts are ready to help solve your<a class ='problem' href='bla.php'>go there</a>. The quality of your question will directly ';

preg_match_all("/href=?[^<]+/i",$str,$lnk);// get link and text

foreach($lnk[0] as $linkdata)
	$link = substr($single_link[0],5);//remove href=
	$link = trim($link,"'\"");//remove double or single quote

	preg_match("/[>]+[a-z,A-Z, ,0-9]+/i",$linkdata,$link_text);
	$text = trim($link_text[0],">");
	print($text . " ==> " .$link . "<br>");

Open in new window

ScottNL1Author Commented:
That is what i was looking for, thanks allot.
both answers i can use.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now