preg_match help

Discussion in 'PHP' started by samirkumardas, Jul 16, 2008.

  1. #1
    $html='<style>
    
    .active {
    	color:#ffffff;
    	background:#0A246A;
    	cursor:default;
    }
    
    #template {
    	text-align:center;
    	width:145px;
    	border:1px solid #aaa;
    	float:left;
    	margin:5px;
    	
    }
    	#template #info {
    		height:100px;
    		width:98%;
    		padding: 2px;
    	}
    
    	#template h6 {
    		margin:0;
    		padding:0;
    		border:0;
    	}
    	
    	#template #use {
    		width:100%;
    		background:#aaa;
    		color:#fff;
    		display:block;
    		font-weight:bold;
    		text-decoration:none;
    		cursor:pointer;
    	}
    	
    	#template #use input {
    		height:10px;
    		width:10px;
    	}
    
    </style>
    
    <script>
    function change_rss(t){
    	f = document.getElementById("rss");
    	if(t == "html"){
    		f.style.display = "none";
    	}else{
    		f.style.display = "inline";
    	}
    }
    </script>
    PHP:

    Hi I need to extract only css class not css ID from this string. For example, from above string, I need a expression so that it gives output

    array('.active')
    PHP:
    becoz .active is the only css class.

    Pls help
     
    samirkumardas, Jul 16, 2008 IP
  2. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #2
    Here is a starting point (I suck bad at regex):

    
    preg_match_all('~\.(.*?)\s~',$html,$matches);
    
    echo '<pre>';
    print_r($matches[0]);
    echo '</pre>';
    PHP:
    Put that under you HTML variable. Unfortunately it picks up anything in the script element too, so you'll need some more help, but its still a start.
     
    blueparukia, Jul 16, 2008 IP
  3. samirkumardas

    samirkumardas Banned

    Messages:
    123
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the help. I need more precise

    Thanks again
     
    samirkumardas, Jul 16, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    
    preg_match_all('~[\s\}>](\.[\w-]+)\s*\{~', $html, $matches);
    
    echo '<pre>', print_r($matches[1], true), '</pre>';
    
    PHP:
    ... this should be pretty precise.
     
    nico_swd, Jul 17, 2008 IP