I have following text
<h2>Subjects</h2>
<ul>
<li><a href='http://isbndb.com/subject/transfer_pricing'>
Transfer pricing</a></li>
<li><a
hef='http://isbndb.com/subject/intangible_property_valuation'>
Intangible property -- Valuation</a></li>
</ul>
the numbers of <li> .. </li> could be variable.
Can anyone tell me the regex to get
Transfer pricing
Intangible property -- Valuation
it might be done with preg_split, but I have no glue how the regex should be.
Thanks a lot.
Try some thing like this:
$output = "<h2>Subjects</h2>
<ul>
<li><a href='http://isbndb.com/subject/transfer_pricing'>
Transfer pricing</a></li>
<li><a
hef='http://isbndb.com/subject/intangible_property_valuation'>
Intangible property -- Valuation</a></li>
</ul>";
preg_match_all("li><a href='(.[^>]*)>(.[^<]*)</",$output , $array_check);
print_r($array_check);
Related
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 2 years ago.
I try to make a regex that only matches list elements over multiple lines with one <li></li> tag.
For example:
<ul>
<li>
Test, Test
</li>
</ul>
this should match, but this:
<ul>
<li>
Test, Test
</li>
<li>
Test, Test
</li>
</ul>
should not.
I already have
<ul>(.*?)<li>(?!<li>)<\/li>(.*?)</ul>
but this don't have a match at all anymore.
Does anybody know how to achieve this?
Don't use regexes for parsing HTML. Here's how to do it without a Regex:
$html = [
'<ul>
<li>
Test, Test
</li>
<li>
Test, Test
</li>
</ul>',
'<ul>
<li>
Test, Test
</li>
</ul>',
];
$previous_value = libxml_use_internal_errors(true);
$dom = new DOMDocument();
foreach($html as $list) {
$dom->loadHTML($list);
echo $dom->getElementsByTagName('li')->length . "\n";
}
libxml_clear_errors();
libxml_use_internal_errors($previous_value);
Demo
This code parses the HTML itself and gets all of <li> elements and then simply counts them.
I have a sample code here
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G</li>
<li>H</li>
<li>I</li>
<li>J</li>
as you can see i am trying to save into session the letter to be sent into sample.php. But even if i press 'B' what gets sent over to the sample.php is 'J' (the last letter) . tried clicking the other letters too its always 'J'.
can anybody help on what i am doing wrong?
Thank you very much
Try this
A</li>
A</li>
A</li>
It’s because your PHP code run and return the HTML output to your browser.
Because of that, all of them are ‘J’.
For solve the problem use code like this:
<li><a href=“sample.php?v=A”>A</a></li>
<li><a href=“sample.php?v=B”>B</a></li>
<li><a href=“sample.php?v=C”>C</a></li>
And you can get v value in your sample.php with this code:
<?php
$linkv=$_GET[“v”];
If ($linkv==“A”)
{
//write your code here
}
If ($linkv==“B”)
{
//write your code here
}
If ($linkv==“C”)
{
//write your code here
}
?>
I'm trying to put a menu together where it will sense what page is loaded. This site is not done in a CMS and is straight PHP/HTML code.
I currently have the navigation working for the primary links. There is a dropdown where I am having problems. I need to be able to see if the parent or any dropdown children are active. If one of the children are active. In the example below this is "FAQ" and the children are "FAQ1," "FAQ2," and "FAQ3."
For this example I'm using a CSS state called "active."
<style>
a{color:red;}
.active{color:blue;}
</style>
Here is the script used for the menu. The links for Home, Products, and Contact are working as expected.
<ul>
<li><a href="index.php" id="homenav" <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="active"';?>>Home</a></li>
<li><a href="products.php" id ="prodnav" <?php if (strpos($_SERVER['PHP_SELF'], 'products.php')) echo 'class="active"';?>>Products</a></li>
<li><a href="faq.php" id ="faqnav" <?php if (strpos($_SERVER['PHP_SELF'], 'faq.php, faq1.php, faq2.php, faq3.php')) echo 'class="active"';?>>FAQ</a>
<ul>
<li>FAQ1</li>
<li>FAQ2</li>
<li>FAQ3</li>
</ul>
</li>
<li><a href="contact.php" id ="connav" <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="active"';?>>contact us</a></li>
</ul>
Can I please get some help on how I should be writing this line to let it work the way the others are?
<li>
<a href="faq.php" id ="faqnav"
<?php
if (strpos($_SERVER['PHP_SELF'], 'faq.php, faq1.php, faq2.php, faq3.php'))
echo 'class="active"';
?>
>FAQ</a>
</li>
strpos() only accepts one string per parameter, you can not give it a list.
Try this:
<?php if (in_array(basename($_SERVER['PHP_SELF']), array('faq.php', 'faq1.php', 'faq2.php', 'faq3.php'))) echo 'class="active"';?>
basename() strips the path from the filename, so you only have the pure file name
in_array() then checks if this path is in an array
array() generates an array of strings to be handed over to in_array(). Note that there are 4 separate strings, not one long one as in your code.
Use in_array for this purpose:
if (in_array($_SERVER['PHP_SELF'], array('faq.php', 'faq1.php', 'faq2.php', 'faq3.php')) echo 'class="active"';
http://php.net/manual/de/function.in-array.php
If you don't have any other pages that contain faq, you can simply use:
if (strpos($_SERVER['PHP_SELF'], 'faq') !== false)
Note that I am using !== false as strpos can return 0 when faq is found at the beginning of your string. You should probably use that for your other comparisons too.
Otherwise, go with the in_array solution of #MrTweek.
I would like to only show the text that is before /*SHOW MORE TEXT*/ However I am unsure how to do such a thing because my text is like this:
<ul>
<li>show this</li>
<li>show this</li>
/*SHOW MORE TEXT*/
<li>don't show this</li>
</ul>
I have tried
$texts = explode("/*SHOW MORE TEXT*/", $text);
But the issue with this is then it won't fix the </ul>
How can I fix this small issue?
Just try with Jquery toggle functions it's more easier and faster. Here you find details: http://api.jquery.com/toggle/
Using php you can do this like
<ul>
<li></li>
<?php
if(condition is true){
echo "<li>text will be displayed if condition is true otherwise it will not displayed</li>";
}
?>
</ul>
using javascript you can use
<li style="display:none;">this will not visible</li>
you can use display:none; property for that and later when you need it you can display it using java script.
<li style="display:none;">don't show this</li>
One possible solution is as follows:
Don't include ul tags in the text string, and then use explode and take the first element of array using array[0] and close </ul> tag.
I am developing a website using PHP and this is the 1st time I am trying a dynamic menu.
What I am doing is I have created a table for pages.
The table structure is as follows:
____________________________________________________________________________
|page_id|title|url|content|menu_title|show_on_navuigation|is_sub|parent_page|
|_______|_____|___|_______|__________|___________________|______|___________|
Here is_sub indicates whether it is a dropdown menu of some main menu. and parent_page is the main menu of the sub menu.
Now, what I want is, like
I have created 2 parent pages:
About Us and Test
and 2 sub menu's: Test aboutus and testsub,
Test aboutus is sub of About Us and testsub is sub of test.
How actually should the query and the loop, so that the menu renders perfectly.
Thanks in advance.
This is my menu structure:
<ul>
<li class='has-sub'><a href='#'><span>About Us</span></a>
<ul>
<li><a href='corporateprofile'><span>Corporate Profile</span></a></li>
<li class='last'><a href='visionandmission'><span>Vision & Mission</span></a></li>
</ul>
</li>
<li class='has-sub'><a href='#'><span>Business Services</span></a>
<ul>
<li><a href='recruitment'><span>Recruitment</span></a></li>
<li><a href='training'><span>Training</span></a></li>
<li><a href='executivesearch'><span>Executive Search</span></a></li>
<li><a href='payroll'><span>Payroll</span></a></li>
<li class='last'><a href='backgroundverification'><span>Background Verification</span></a></li>
</ul>
</li>
<li class='has-sub'><a href='#'><span>Employers</span></a>
<ul>
<li><a href='enquiry'><span>Enquiry</span></a></li>
<li><a href='jobdescription'><span>Job Description</span></a></li>
<li><a href='employercontract'><span>Employer Contract</span></a></li>
<li class='last'><a href='feedback'><span>Feedback</span></a></li>
</ul>
</li>
<li class='has-sub'><a href='javascript:;'><span>Job Seeker</span></a>
<ul>
<li><a href='applyforjob'><span>Apply For Job/Register</span></a></li>
<li><a href='careertips'><span>Career Tips</span></a></li>
<li><a href='interview Questions'><span>Interview Questions</span></a></li>
<li><a href='interviewprocess'><span>Interview Process</span></a></li>
<li class='last'><a href='corporatedress'><span>Corporate Dress</span></a></li>
</ul>
</li>
<li class='has-sub'><a href='#'><span>Franchise</span></a>
<ul>
<li class='last'><a href='#'><span>Franchise Enquiry Form</span></a></li>
</ul>
</li>
<li class='last'><a href='#'><span>Contact us</span></a></li>
</ul>
<?php
function displayMenu($parent_page_id) {
$sql = "SELECT * FROM `pages` WHERE `parent_page` = '$parent_page_id'"; // sql
$result = mysql_query($sql);
if( mysql_num_rows($result) === 0 ) { // mysql_num_rows() is deprecated, but you are using mysql extension so that's why I show it here
return true; // exit function
} // else, continue to rest of function
echo '<ul>';
while($row = mysql_fetch_assoc($result)) { // deprecated mysql php function here for simplicity
echo '<li>' . $result['menu_title'] . ''; // no closing </li> yet
displayMenu($row['page_id']); // this is the recursive part
echo '</li>'; // close the <li> from before the recursion
}
echo '</ul>';
}
$get_base_menu = mysql_query("SELECT * FROM pages WHERE parent_page = 0");
while($fetch_base_menu = mysql_fetch_array($get_base_menu))
{
$parent_page_id = $fetch_base_menu['page_id'];
displayMenu($parent_page_id);
}
?>
I highly suggest trying to get away from using an Adjacency List model and move toward a much easier to manage solution, such as a nested set. Using an MPTT type solution should help you manage your hierarchical data much easier. Using an Adjacency List model you are limited at a certain point.
I'd suggest looking into using something along the lines of Zebra_MPTT, or some other form of MPTT library. Please checkout this article on Managing Hierarchical data in MySQL.
I think the simplest thing for you to code will be something like this recursive function. All you have to do is put it on your page and make sure you have parent_page_id = 0 for the base level menu items. (I'm using parent_page_id instead of parent_page because I assume that is what you mean and it is more clear for the answer.)
Calling the function with an argument of "0" should give you the full menu tree.
$trace = array();
function displayMenu($parent_page_id) {
$sql = "SELECT * FROM `tbl_menu` WHERE `parent_page_id` = $parent_page_id"; // sql
$result = mysql_query($sql);
if( mysql_num_rows($result) === 0 ) { // mysql_num_rows() is deprecated, but you are using mysql extension so that's why I show it here
$trace[] = "Page_id $parent_page_id has no children";
return true; // exit function
} // else, continue to rest of function
echo '<ul>';
while($row = mysql_fetch_assoc($result)) { // deprecated mysql php function here for simplicity
$trace[] = "Entered while loop for page_id = $parent_page_id"; // not an error, just tracking
echo '<li>' . $row['menu_title'] . ''; // no closing </li> yet
displayMenu($row['page_id']); // this is the recursive part
echo '</li>'; // close the <li> from before the recursion
}
echo '</ul>';
}
displayMenu(0); // call function for base level
var_dump($trace);
More info:
I have researched the topic before and had a nice link to a page on mysql.com with an in-depth exploration of hierarchical relational database structures. But when I checked, the link was broken!
But, I think I found it on another site, here:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/